Practical Guide to Python

Boolean Logic & Control Statements

Nina Zakharenko

Nina Zakharenko

Microsoft
Practical Guide to Python

Check out a free preview of the full Practical Guide to Python course

The "Boolean Logic & Control Statements" Lesson is part of the full, Practical Guide to Python course featured in this preview video. Here's what you'd learn in this lesson:

Nina demonstrates boolean operators and controls statements like if, elif, and else. The "is" keyword best for checking identity between two objects, not equality. If/Else statements use a colon and indentation similar to function blocks.

Preview
Close

Transcript from the "Boolean Logic & Control Statements" Lesson

[00:00:00]
>> So, Boolean Logic in Python. You might have heard this expression, truthiness. Truthiness is figuring out what expressions evaluate to true or false in Python. And that's going to help us control the flow of our program. Now I have a little handy cheat sheet for you here at the top.

[00:00:25]
With integers, truthiness works a little bit differently than you might expect. The truthiness of integers, 0 is false. All other numbers that have a value are true, including negative numbers. For container types, like the list, the tuple, the set, the dictionary. Empty containers evaluate to false containers with items in them evaluate to true.

[00:00:55]
Now the non or null type is always false. There are also keyword arguments of true with a capital T and false with a capital F. Now, sometimes truthiness is pretty obvious is this true or false? It's True, right. If I tried it the reverse way, that's False. You can always figure out what truthiness your expression will have by calling bool on it.

[00:01:38]
And bool maps to the constructor for the built in True or False. So, I can call bool with 1 which is True, bool with 0 which is False, but I can also pass in some complex expressions here. And it will let me know what the truthiness value of them is.

[00:02:03]
So, calling bool on an empty list False, putting some items into it is True. Bool of none type is always False. You can compare different items in Python with less than, greater than, less than or equals. But there's also the double equals and the not equals. So, double equals is equality.

[00:02:39]
This is not equals. Equality for different container types is a little bit interesting. So, equality for list will check for the items in the list. So, let's say I have list1 and list2. If I call list1 equals list2, I'm checking for equality of the contents. And I'm not asking, is this the same exact list.

[00:03:20]
If you do want to check for, hey, are these lists pointing at the same variable? That is, you're gonna use a different keyword called is, that checks for identity. Basically asking, do these points the same place in memory? They don't, they're stored at different variables. So, this is a little bit of a more advanced topic.

[00:04:00]
But the reason that I'm introducing it is because we have a lot of keywords in Python that we've seen, like True and False and none. In order to check for equality with these keywords, we want to use the is keyword to check for identity. Do these things point to the same place in memory and not with equality?

[00:04:32]
This is especially handy with none because a empty list can evaluate to False. So, if we want to differentiate between non and an empty list, we need to use the is keyword. And we can say x is none. And that is True. If I ask, empty list is none that is False.

[00:05:10]
So, if you need to differentiate between the truthiness of a none value or something that has a false truthiness like an empty list or zero. You're going to need to use that is keyword. And same for True and False. So, don't do a equals True. If you want to know explicitly, you can say a is True, oops.

[00:05:45]
I would need to define that. And that will say True. So, the difference between equality and identity. There's also the and, or and not statements in Python. One of the great things about Python is that these statements are just English keywords. And it kind of spells out like you would type a sentence, for those of you who might come from Java, or another language, you don't have to do these stuff.

[00:06:25]
It's super hard to remember when you're a beginner, so I think having these keywords definitely helps out with learning Python. And let's say a equals True, and b equals True, and means are both of these values True? Only return True when they're both True. If one of the values is False, that will return False.

[00:06:54]
Or means, are any of these values True so True or False will return True, True and True will return True but False or False is False. Not just negates what you already have. So, if a is True, then not a is False. And you can also use these things in combination.

[00:07:25]
And make sure that you maintain the order that you want with parentheses. So, I can say True and True or False and that will return True. We can then go ahead and use these in if statements. So, if statements will only run if the expression is True, not if the expression is False.

[00:07:55]
To define if statements, you just type the if keyword followed by an expression that evaluates to True or False. If you're not sure if your expression evaluates to True or False, you can pass it into bool. So, I can say if 3 is less than 5. And that will run because my expression is True.

[00:08:21]
If I try to do the same thing, but reverse the order, 3 is, sorry, I wanted to ask is 5 less than 3, this is False. And so, this will never run. You can use and or not, all of those inside of the expression in your if statement.

[00:08:50]
So, if I have a is True and b equals False, I can say if not b. Will this print out Hi or not? It will, right, because b is False I've now negated it in my if statement. You want to be mindful of if statements and truthiness, all of those same things that we talked about earlier apply.

[00:09:18]
So, if I try to say, if empty list print Hi, the truthiness of an empty list is False. And so nothing is going to happen. I can put some values in here and then that will print Hi. Same for zero is False, any number including negatives is true.

[00:09:40]
And if I have a variable like this b is empty list, I can just say if b print Hi, oops. So, pretty important Python syntax, I don't have to say if b is double equals to emptying list or anything like that. Because what really matters is the truthiness of the expression.

[00:10:09]
So, that's a pretty important concept. We want to be mindful of not using that double equals True in our if statements. So, I don't wanna say if b double equals True don't do this. Print Hi, well it might work, that is not correct Python syntax. We can add an all statement to our if statement and that will only run if the if statement itself did not run.

[00:10:48]
I Just wanna show you the syntax for that in Python. So, if 5 is less than 3, print, Hi. Else goes right beneath that if statement with a colon. There's no expression for else, because else only runs if the if statement is not true. I also want to indent that.

[00:11:16]
And now my else statement is going to run.
>> I actually had a question on dicts, is it possible to delete a key I saw instances of you overwriting one but I wasn't sure if you could.
>> Yes, you can delete a key from a dictionary with the del keyword.

[00:11:36]
You can also pop from a dictionary. So, let's quickly take a look at that. Oops, I need to use curly braces. So, my dictionary now has these keys in it. And I can pass in which one to delete. I recommend not using this syntax even though you might see it.

[00:12:14]
I think it's kind of weird, I prefer pop instead. Because pop also returns the value of the key that you just deleted. And if you need to know more about pop, you can look at help on dict.pop. You can also provide a defaults.
>> So, you would have a default one to pop that's kind of.

[00:12:44]
>> Yeah,
>> The default would be what's returned by the pop.
>> If the key is not found. So, kind of, if you don't wanna take that extra step of checking for an exception. Because if I tried to delete from my dictionary a key that does not exist, I'm going to get a key error.

[00:13:08]
So, if you don't want to do any extra exception handling, you can use pop. And pass in a default. To return if that key isn't found instead of throwing an exception.
>> I have a question regarding, so I'm little confused about the whole equals, equals.
>> Yeah.
>> For example, I put this in the Zoom chat three is less than five equals, equals to True.

[00:13:39]
The value is False.
>> Yeah, you don't want to do that because generally, expressions have their own truthiness. And so, you don't have to use that double equals to compare it to True or False. It just happens under the hood. And you can double check that by passing an e-value, or an expression to bool.

[00:14:08]
So, I can say if 3 is less than 5, what's happening here is that Python is replacing this expression with the result of this under the hood.
>> Okay, I think I understand why I don't have to do that.
>> Mm-hm.
>> But I don't think I understand why the result seems to be countered to what I believe it should be.

[00:14:41]
>> I'm not 100% sure, because generally I know it's best practice not to do this and so I don't. I think there's a weird order of operations thing happening here, where this is getting evaluated first and this is getting evaluated second. I think that's why it's returning False.

[00:15:00]
So, it's saying is 5 equals to True or False is 3 less than False. Remember False is actually a number, False is 0 in Python. So, then this expression becomes is 3 less than 0 which is False. So, you end up with some weird subtle bugs.

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