Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Booleans7:51 with Craig Dennis
Let's take a look at boolean literals True and False and how to use logic
A boolean data type only has two valid values, true and false. 0:00 These values are gonna be use heavily in the code that we create from here on out. 0:05 There are many times where we'll wanna ask a yes or 0:09 no question and then run different code based on how the question's answered. 0:11 Booleans are named after their creator, a mathematician named George Boole. 0:16 In George's algebra, he used ones to represent the value true and 0:20 zeros to represent false. 0:25 And this is the reason you see actors playing hackers in movies dealing with 0:27 ones and zeros. 0:31 There's a popular saying in this world that says, it's all ones and zeros. 0:32 And that's because it is. 0:36 Electrical circuits used in the computer you're using right now, 0:37 are using boolean values, on or off, true or false, one or zero. 0:41 So, they seem pretty important to get a handle on, right? 0:44 We should explore these, true or false? 0:48 The answer is true. 0:51 There are only two boolean literals, true and false. 0:52 A variable can refer to the boolean result in an expression like our in keyword. 0:58 For instance, has_taco = is taco in catacombs. 1:03 You'll see that that was stored. 1:10 Has_taco, is stored true in there. 1:13 And just like our other data types, 1:16 you can actually coerce values to be true or false. 1:18 It's called bool and as you probably guessed, 1:22 bool(1) is true and of course bool(0) is false. 1:27 Now here's something that might surprise you. 1:34 In addition to being the answer to the meaning of life, 42 is also true. 1:36 In fact, any non-zero number is true, and zero is false. 1:42 So, what happens with the string? 1:47 So if I say, what's the boolean of burrito. 1:50 Now I guess speaking personally in this statement, I want a burrito, 1:58 is always gonna return true, but that's not why this is true. 2:01 What's happening here is any object that isn't empty is true. 2:04 Now, empty is an interesting concept that we haven't talked about just yet. 2:09 So, a string literal is a pair of quotes surrounding a character, right? 2:13 But what if there aren't any characters in there. 2:18 Now this is known as an empty string, and as you can see, it's false. 2:23 This emptiness is false, tends to be a common confusion point for 2:28 developers, so I want to introduce you to another term. 2:32 The way in which a value coerces to a boolean also has a name, 2:36 it's truthy or falsey. 2:41 So, for instance, I can say that empty string is falsey, and 2:44 the number seven is truthy. 2:48 I know it sounds like I made that word up but I assure you it's real, 2:50 and you'll see it and hear it used. 2:54 I guess it's slightly better than saying it's true-ish or false-ish. 2:56 So all that to say, emptiness is falsey. 3:01 [LAUGH] Sounds like the name of an emo band. 3:04 I'd like to introduce you to a couple of keywords that you already know and 3:09 use in real life. 3:12 You are about to see some of the beauty of Python's readable syntax. 3:14 So, if I wanted to negate a boolean value, you know, 3:19 get the opposite of the value, I can use the not keyword. 3:22 So I can say not True, and it says False. 3:26 Neat, right? 3:31 And if I say not False, we'll see that that's actually True. 3:31 So next up, you can actually chain together booleans using the keyword and. 3:36 So if we say, True and True, we see that that's True. 3:44 The way that and works is that both booleans on both sides of the and 3:49 must be true. 3:54 So, you can keep chaining them together, too. 3:55 And it works just like order of operations on our math examples, 3:57 it goes from left to right. 4:01 So if I say True and True and True, we get back True. 4:03 But what happened was it checked this True and True, and that returned True. 4:10 So that True, the result of this was checked against this, True and True and 4:15 both sides were true so therefore it's true. 4:19 But, if we come and we slap a false at the very end, and False. 4:21 We'll see that it's false because all of this became true, and then we had true and 4:26 false, and both sides must be true in order for and to return true. 4:31 And it wasn't, so it returned false. 4:34 So ors work a little bit different. 4:35 If either value on either side of the keyword, or is true, it's true then. 4:38 Ands work with both and 4:43 ors work with either. 4:48 So if it's False or True, we're gonna end up with True, because there's one true. 4:53 But if we have False or False, it's gonna be False cuz nothing's true. 4:57 You can chain ors together too and you can also use params just like we did with our 5:04 math to help that be more specific about the order of what's happening. 5:08 So let's just go ahead let's build something random. 5:12 We'll say (False or False or 5:15 True) and (True and False). 5:21 Okay, so let's walk through it before we press Enter here. 5:28 So, two falses altogether that's a false. 5:30 So we have a False or True. 5:34 One of those is true, so we have a true. 5:36 So True and, and (True and False), so this is a false. 5:38 So we have True here and 5:42 we have False over here because not both of these are true. 5:43 So we have a False and a True, that should be False. 5:47 Because both sides need to be true. 5:53 And remember, we can always negate. 5:55 We come in here, we can say, and not. 5:59 Now you'll see that it looks like I typed over the front. 6:03 This is a bug that sometimes happens in the repl. 6:05 So I'm gonna press Spacebar. 6:08 Even though that looks wrong, watch. 6:10 Press Enter, it came true. 6:12 If we look at it, there it is. 6:14 So we have false, or false, or true and not. 6:15 And so that negated this last one here, so this not true or false, true and 6:18 false was false. 6:22 Not false is true. 6:23 I know that this is all a little bit abstract. 6:25 So, let's try to make this a little more concrete. 6:29 Let's think about a dating app. 6:32 And let's try to imagine some code that might exist there. 6:35 So, Heidi has some kids. 6:39 And she'd like to find someone who has kids too. 6:41 But she can't smoking, she abhors it. 6:44 So, let's fill out a profile for a parent on the site who's also a smoker. 6:47 So, is_smoker = True, and 6:52 their parents, so has_kids = True. 6:56 So Heidi's requirements look like this. 7:01 She says I want somebody who's a parent. 7:04 So he has_kids and is not a smoker. 7:05 So, we have in this particular situation, for her requirements, this profile here, 7:13 we have has_kids, that's true and not is_smoker, so that's false. 7:19 So we have true and false, that's a false, that didn't work out, 7:24 Heidi is gonna swipe left. 7:28 That's just isn't the one for here I guess, he's hoping that she finds her. 7:30 Do you see the power of booleans? 7:34 They can help you find true love. 7:36 Now, to really fall for booleans, I'd like to show off the ability to run different 7:40 blocks of code based on their value. 7:44 This is called conditional branching. 7:46 Let's branch off to that topic, right after this quick break. 7:48
You need to sign up for Treehouse in order to download course files.Sign up