Python Fundamentals

and, or, & not

Python Fundamentals

Check out a free preview of the full Python Fundamentals course

The "and, or, & not" Lesson is part of the full, Python Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Nina introduces boolean logic built in to Python.


Transcript from the "and, or, & not" Lesson

>> Nina Zakharenko: Okay, lastly, and, or, & not. If you've done computer science, you probably seen some truth tables in your day, Python works a little bit different. Kind of, the basic values evaluate the same, but,
>> Nina Zakharenko: I have a bit of cheat sheet here, and this kind of comes into play when you're evaluating things that have a truthiness to them.

If you're just kind of evaluating True and False, the answer is obvious. Otherwise, you wanna take a quick look at this cheat sheet. Let's just see this in practice.
>> Nina Zakharenko: Let's say a is true and b is true,
>> Nina Zakharenko: a and b, so and, when you're comparing two true values in a truth table, kind of know that both values a and b have to be true, in order for True to return.

But because a is true here, we're returning the value of b, which just happens to be True.
>> Nina Zakharenko: This makes more sense, let's look at something with a truthiness value.
>> Nina Zakharenko: All right, so both of these lists with items in them are truthy. When doing list 1 and list 2, the first one is True, and so we're gonna return the second one, so a little bit of a trickiness to it.

Let's say a is False and b is True, if we do a and b, it's gonna return False, so that's the value of a. And then if a is False and b is False, and we do a and b, False is returned again, that's the value of a.

So if I had an empty list and an empty dictionary, we would see the empty list returned. If I had an empty list, false-y value, and a set with one item in it, it would still return that first one. I still kind of forget this stuff, so that's why the table is super helpful.

Okay, so for or, if a is True and b is True, a or b, that returns the value of a. So quickly, with truthiness, that returns the value of a. If a is False and b is True, a or b will return the value of b, right? So if this is False and this is True, it'll return the True item.

And if we look at the numbers, remember, 0 is False, and any other number is True, this will return the True value. And not is just the inversion, and not will return True or False, it doesn't return any specific value. So not False is True, not True is False, just the negation.

Again, we can use bool to kind of check, we know that bool(1) is True, so not 1 is False. bool(0) is False, not 0 is gonna be what, not False?
>> Student: True.
>> Nina Zakharenko: True, exactly right, yeah, and you can kind of use these things in combination, so there's an order of operations here.

And if you want to kind of be very specific, this is where you can use parentheses.

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