Python Fundamentals

# Booleans

## Nina Zakharenko

Microsoft

### Check out a free preview of the full Python Fundamentals course

The "Booleans" 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 truthyness and falseyness in Python.

Preview
Close

### Transcript from the "Booleans" Lesson

[00:00:00]
>> Nina Zakharenko: Let's move on to Boolean logic. We said a little bit of this at the beginning of class, when I talked about not using an empty list as a default keyword agreement. You saw me use some if statements. So we're gonna cover Boolean logic, and you'll kind of get a better idea of how that works under the hood in Python.

[00:00:24]
Someone in the chat was asking about truthiness, so we've seen the values True and False. Those are kind of black and white, True is true, False is false. But in Python, truthiness is, for a particular type, is the value represented as true or false under the hood? And the best way to do that is to look at Terminal.

[00:00:56]
>> Nina Zakharenko: Okay, so for integers,
>> Nina Zakharenko: Well, that's kind of, okay, let's see, there's a built-in type True, right, built-in type False, those things are just true or false.
>> Nina Zakharenko: The shorthand for figuring out the truthiness of a value, so if we look at the type of True, we'll see that it's a bool.

[00:01:24]
We can use this type, and pass things into it, and it will kind of give us a hint of if they're evaluated to true or false. I'm just showing you this in the REPL now, so we can see things live, as I type. In general, you don't need to pass these expressions to bool, because that conversion happens under the hood, okay?

[00:01:49]
So for numbers, the number 0 is false, or in this case, false-y. Any other number in Python is truthy, including negative numbers, so kind of a gotcha. Lists, container, lists, tuples, sets, empty lists are false-y, so are empty dictionaries, empty tuples, and empty sets. Those are all false-y, they're empty, if the container has any items in it, then it evaluates to true.

[00:02:46]
So a list with an item in it, truthy, a tuple with an item in it, truthy, a set with an item in it is truthy. And lastly, a dictionary with an item in it is truthy, make sense? 0 is falsy, all other numbers including negatives are truthy, empty containers are false-y, containers with items in them are truthy.

[00:03:21]
And then the None type, just like you'd kind of expect, the None type is false-y.
>> Nina Zakharenko: Don't forget not to name your variables True, False, None, those are keywords.
>> Nina Zakharenko: Remember that Python kind of considers a string to be a sequence as well. We saw that when we tried to update a set with a string, it just took all the individual items and added them in.

[00:04:00]
So just like with a sequence, an empty string is false-y, but a string with characters in it is truthy.

### Learn Straight from the Experts Who Shape the Modern Web

• In-depth Courses