Python Fundamentals

Practice: Boolean Logic

Python Fundamentals

Check out a free preview of the full Python Fundamentals course

The "Practice: Boolean Logic" Lesson is part of the full, Python Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Nina live code the exercise with students.

Preview
Close

Transcript from the "Practice: Boolean Logic" Lesson

[00:00:06]
>> Nina Zakharenko: Let's go through our exercise for boolean logic. Taking a look at vs code. Doing some comparisons, comparisons with numbers. You know, 5 < 10. We kind of saw all of that. The interesting ones here are equality is 5 == 5, yes. Is 5 != 5, false, right?

[00:00:32]
But there are truthiness values kind of underlying truthiness to some of these numbers. So kind of where this makes sense is let's say we had a very simple if statement, which we'll cover in a second. But the truthiness of numbers other than 0 is true.
>> Nina Zakharenko: And so using an if statement, this will evaluate to true, so just kind of a quick and handy way to check truthiness values.

[00:01:09]
Under the hood, true and false are represented by 1 and 0. So this is just kind of an interesting factoid, so 1 == True, 0 == False. We'll talk about why that is tomorrow, but somebody in the chat figured that out so I thought that was fun. For and operations, for X or Y, if X is false, then we're going to return Y.

[00:01:38]
So if I have an empty list or a list with an item in it, so a falsey or a truthy, it will return the truthy value using the or statement. Or if I use, sorry that's not a good example, let's say it was a,
>> Nina Zakharenko: Falsey or truthy.

[00:02:06]
But then truthy or truthy, it's gonna return that first true value. For and, if the first value is false, it's gonna return that. Otherwise it's gonna return the other value. So for empty list and empty set we are gonna get back an empty OS. So the first false value.

[00:02:31]
So False and True false.
>> Nina Zakharenko: If we do two true values. And we and them together. It's going to return the second one. Not returns a Boolean true or false depending on the truthiness of the variable in question. So not false true, not empty list. That's the same thing going on under the hood.

[00:03:05]
What will not empty list evaluate to? Empty list is falsy, not falsy, true, exactly right.
>> Nina Zakharenko: And of course with strings, strings are truthy. So if we did this truthy string and none which is a falsy type.
>> Nina Zakharenko: It will return none, right, nothing.
>> Nina Zakharenko: Okay.
>> Nina Zakharenko: And you're not limited to two operations, let's say a was true, b was true, and c was true.

[00:03:48]
You can do a and b and c, and these conditionals will get more complicated as your programs get more complicated.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now