课程: Python Essential Training

Booleans

- [Instructor] We looked at Booleans earlier, and you might have been left with the impression that there's not a lot left to say about them. True and false, right, how hard could it be? Well, there are a few actually tricky aspects of them that we need to cover. And because I'll use them day in, and day out as a programmer, it's extremely important to cover these. Honestly, I occasionally find myself bringing up a Python terminal to double check a line of boolean logic or confirm exactly how Python handles one case or another. So let's go to the code. The first thing I want to talk about is casting. So Python will cast integers to booleans pretty nicely. 1 is true, and of course 0 is false. In fact, anything except a 0 will be cast a boolean true. So something like -1 might feel false to us, but be careful to Python, it's true. Even something like let's do imaginary one, that's true. However, imaginary zero or float zero are both false. So 0.0, and if we do zero j, they're false. Okay, so what about strings? Boolean true is true, fair enough. Boolean false is true. That's because anything other than an empty string is actually going to be true. There's nothing special about this string false. It's just a string, python assumes that it's true. Of course an empty string is the only false string. But be careful even putting something like a space in here, make sure that it's truly an empty string. What about data structures? We can cast these to Booleans as well. An empty list is false. An empty dictionary is false. If I put anything inside of here though, it's true. Remember that non-value that Python returns from functions that if you don't add any explicit return value there, well, unsurprisingly, these get cast to false. Now, why is it so important to learn the rules of casting Booleans and Python? Well, remember that Booleans aren't usually used directly. You're usually checking the boolean value of statements inside an if statement or a for loop. So you'll very often use them in situations like this. My list equals 1,2 if my list print "My list has some values in it", great. And what we're doing here is casting the value of my list to boolean in order to evaluate. This is equivalent to writing this. You can also do, something like this. a equals five, b is equal to five. If a minus b, print, "a and b are not equal". Great, didn't print, the only way that a minus b evaluates to zero or false as if a is equal to b. Of course, this is a little overly clever. If we really want to do this, we could just do that. And that brings me to my next point. There's usually more than one way to evaluate boolean. So you have to be a little bit careful about keeping track of this logic. So let's look at a situation where we're evaluating, say, whether or not to go for a walk. So weather is nice, is false, have umbrella is true. So if weather is nice or we have an umbrella, we can go for a walk. If not weather is nice or have an umbrella, we must stay inside. So if not have umbrella, or weather is nice, print, "Stay inside", else, print, "Go for a walk". So if we have an umbrella, this tells us we can go for a walk. Looks good, right? But python evaluates booleans left to right. So this is saying not have an umbrella which is false, or weather is nice, which is also false. So this whole thing evaluates to false. There are actually imaginary parenthesis around the not have an umbrella here. So this isn't quite the logic we want. If we flipped the booleans so that we don't have an umbrella, but the weather is nice, you can see that it does the wrong thing. It's telling us to stay inside even though the weather is nice. So there are a couple ways to fix this, and fix this in a way that would give us the correct logic. So first, we could do this. We could put parentheses around the thing that we want to evaluate first. And this would give us the correct logic. There's also kind of a cool thing we could do which is rewrite this with an and statement. So let me just copy this. If not have an umbrella and not weather is nice. Let's do that, and this is actually the exact same logic we had before. These two are equivalent. Although it's not strictly necessary, it might be nice to put parenthesis around this just for readability, like so. But that's just readability for humans, not computers. And of course we could also flip the order of these statements entirely, which is probably the best, and most readable way in my opinion. So if have umbrella or weather is nice, print "Go for a walk" else print, "Stay inside." So yes, Booleans are at a certain level, very simple. However, it's critically important that when you're writing a boolean statement, you'd be very careful. Think about how Python is evaluating each piece of the statement, and always double check your logic.

内容