1 00:00:00,510 --> 00:00:01,680 Instructor: Welcome back. 2 00:00:01,680 --> 00:00:03,090 In this video, I wanna talk 3 00:00:03,090 --> 00:00:07,353 about the idea of truthy and falsy. 4 00:00:08,820 --> 00:00:10,620 What do they mean? 5 00:00:10,620 --> 00:00:13,470 And you'll hear this a lot in Python programming. 6 00:00:13,470 --> 00:00:18,300 You see, up until now, you've used booleans here as values. 7 00:00:18,300 --> 00:00:19,470 So it was really easy. 8 00:00:19,470 --> 00:00:23,430 If true and true, then run this code. 9 00:00:23,430 --> 00:00:26,460 But Python can do other things as well. 10 00:00:26,460 --> 00:00:31,460 For example, if this was five and this was, let's say hello, 11 00:00:32,910 --> 00:00:35,940 what do you think will happen here in our program? 12 00:00:35,940 --> 00:00:37,533 Well, if I click run, 13 00:00:39,630 --> 00:00:41,010 you are old enough to drive 14 00:00:41,010 --> 00:00:44,223 and you have a license, is that what you expected? 15 00:00:45,660 --> 00:00:48,660 And this is because what Python does 16 00:00:48,660 --> 00:00:51,720 underneath the hood is actually convert these 17 00:00:51,720 --> 00:00:53,700 to booleans for you. 18 00:00:53,700 --> 00:00:56,310 So it's almost as if 19 00:00:56,310 --> 00:00:59,880 it is doing this, remember type conversion. 20 00:00:59,880 --> 00:01:01,390 We're converting the type 21 00:01:02,280 --> 00:01:04,440 to boolean 22 00:01:04,440 --> 00:01:06,240 as soon as we get to this if statement 23 00:01:06,240 --> 00:01:08,190 because Python is saying, Hey 24 00:01:08,190 --> 00:01:09,960 I want true or false, I don't care 25 00:01:09,960 --> 00:01:10,890 what is old is 26 00:01:10,890 --> 00:01:13,770 I don't care what is licensed is just gimme true or false. 27 00:01:13,770 --> 00:01:17,343 So I know whether to do this or to go to the else block. 28 00:01:18,660 --> 00:01:21,810 Now, if I was to just print, 29 00:01:21,810 --> 00:01:23,070 let's do this here. 30 00:01:23,070 --> 00:01:24,430 Let's do print 31 00:01:25,320 --> 00:01:26,580 boolean 32 00:01:26,580 --> 00:01:28,200 hello. 33 00:01:28,200 --> 00:01:30,850 And then finally print boolean 34 00:01:32,250 --> 00:01:33,090 five. 35 00:01:33,090 --> 00:01:34,190 Let's see what we get. 36 00:01:36,210 --> 00:01:38,910 We get true, true. 37 00:01:38,910 --> 00:01:43,770 And this is what we call a truthy value in Python. 38 00:01:43,770 --> 00:01:47,873 That is, it's a truthy value because if we run the boolean 39 00:01:47,873 --> 00:01:52,620 type conversion on it, it evaluates into true. 40 00:01:52,620 --> 00:01:54,330 What's a falsy value? 41 00:01:54,330 --> 00:01:59,010 Well, if I do zero, and if I do, let's say an empty string 42 00:01:59,010 --> 00:02:00,393 and I click run, 43 00:02:01,680 --> 00:02:03,300 those are falsy. 44 00:02:03,300 --> 00:02:08,056 That is, it's not actually false, but it is falsy because 45 00:02:08,056 --> 00:02:12,060 if we run boolean on it to Python, it's going to say, Oh 46 00:02:12,060 --> 00:02:14,160 this is false and zero. 47 00:02:14,160 --> 00:02:14,993 That's false. 48 00:02:14,993 --> 00:02:17,073 And that kind of makes sense, right? 49 00:02:18,120 --> 00:02:21,900 And if you actually go to Stack Overflow, if you've never 50 00:02:21,900 --> 00:02:25,020 seen Stack Overflow before, if you ever have programming 51 00:02:25,020 --> 00:02:29,160 questions and you're Googling them, usually the top five 52 00:02:29,160 --> 00:02:32,010 answers we'll contain either one or two 53 00:02:32,010 --> 00:02:33,960 Stack Overflow answers. 54 00:02:33,960 --> 00:02:37,170 And basically what happens is people ask questions like, 55 00:02:37,170 --> 00:02:39,180 What is truthy and falsy and Python? 56 00:02:39,180 --> 00:02:41,340 And you get answers. 57 00:02:41,340 --> 00:02:44,310 So you can see over here that it has four answers 58 00:02:44,310 --> 00:02:47,310 and you usually look for the check mark, which means 59 00:02:47,310 --> 00:02:48,840 that this is a verified answer. 60 00:02:48,840 --> 00:02:52,050 It means that, hey, this question owner has accepted it 61 00:02:52,050 --> 00:02:52,980 as the best answer. 62 00:02:52,980 --> 00:02:55,770 So the person who asked this question just said, Yep 63 00:02:55,770 --> 00:02:56,730 this is the right answer. 64 00:02:56,730 --> 00:02:57,840 This is the best one. 65 00:02:57,840 --> 00:02:59,520 But you also have other people voting. 66 00:02:59,520 --> 00:03:03,450 So you can see that this answer received 46 marks. 67 00:03:03,450 --> 00:03:04,800 And although it was marked 68 00:03:04,800 --> 00:03:08,280 as the correct answer by the user, maybe somebody else came 69 00:03:08,280 --> 00:03:11,430 along later on and gave an even better answer. 70 00:03:11,430 --> 00:03:14,760 So now we have 123 up votes. 71 00:03:14,760 --> 00:03:17,430 And you see that this answer is a lot better. 72 00:03:17,430 --> 00:03:21,213 All values are considered truthy except for the following. 73 00:03:22,260 --> 00:03:25,470 So you can actually see here that in Python 74 00:03:25,470 --> 00:03:29,130 these values are considered falsy. 75 00:03:29,130 --> 00:03:32,760 You can see zero here, you can see an empty string here 76 00:03:32,760 --> 00:03:34,740 and then there's a few other ones. 77 00:03:34,740 --> 00:03:36,060 Empty set. 78 00:03:36,060 --> 00:03:40,416 You have a empty dictionary, empty list, empty tuple. 79 00:03:40,416 --> 00:03:44,310 You have decimals, you have things like fractions. 80 00:03:44,310 --> 00:03:46,080 You have 0.0. 81 00:03:46,080 --> 00:03:47,130 False is that. 82 00:03:47,130 --> 00:03:50,280 And also none is falsy. 83 00:03:50,280 --> 00:03:53,313 So that if I type in none here, 84 00:03:54,630 --> 00:03:56,043 that's false too. 85 00:03:57,210 --> 00:03:58,800 Now, you don't need to memorize these 86 00:03:58,800 --> 00:04:02,190 because most of the time it, it makes sense, right? 87 00:04:02,190 --> 00:04:05,490 But it helps us do conditional logic very nicely. 88 00:04:05,490 --> 00:04:08,580 For example, let's remove this. 89 00:04:08,580 --> 00:04:13,110 And let's say that we have a user 90 00:04:13,110 --> 00:04:18,110 and this user has to have a password and a username. 91 00:04:21,029 --> 00:04:23,560 If let's say the username is 92 00:04:24,810 --> 00:04:25,800 Johnny 93 00:04:25,800 --> 00:04:29,403 and the password is 1, 2, 3. 94 00:04:30,930 --> 00:04:33,000 Well, instead of me saying, 95 00:04:33,000 --> 00:04:36,840 Hey, if this user has password and username 96 00:04:36,840 --> 00:04:39,270 and we're not checking if the username exists 97 00:04:39,270 --> 00:04:41,190 or password exists, we just wanna make sure, 98 00:04:41,190 --> 00:04:43,530 hey did this person fill out the form 99 00:04:43,530 --> 00:04:46,560 and did they try to submit a username and password? 100 00:04:46,560 --> 00:04:50,410 I can just simply say password and username 101 00:04:51,960 --> 00:04:53,010 and I don't have to worry 102 00:04:53,010 --> 00:04:55,530 about converting this to a boolean or anything like that. 103 00:04:55,530 --> 00:04:58,650 Because if password and username exists 104 00:04:58,650 --> 00:05:00,570 then try to log them in. 105 00:05:00,570 --> 00:05:03,690 And you see that it reads a lot nicer 106 00:05:03,690 --> 00:05:06,300 because each one of these get evaluated 107 00:05:06,300 --> 00:05:08,880 into a truthy and falsy value. 108 00:05:08,880 --> 00:05:11,521 It's a nice little feature to keep Python, 109 00:05:11,521 --> 00:05:14,763 well, nice and simple, and reading like English. 110 00:05:15,750 --> 00:05:17,160 I'll see you in the next video. 111 00:05:17,160 --> 00:05:17,993 Bye bye.