1 00:00:00,000 --> 00:00:03,000 Let's take a look at multiple comparison operators 2 00:00:03,000 --> 00:00:04,200 within an 'if' statement. 3 00:00:04,200 --> 00:00:10,000 So in the last two videos I said, if something is True, do a thing. 4 00:00:10,000 --> 00:00:12,800 So I am just going to write 'pass', because indentation is important. 5 00:00:12,800 --> 00:00:16,600 If we just wrote 'if True', we're going to see 'SyntaxError', 6 00:00:16,600 --> 00:00:18,400 so we just write 'pass' 7 00:00:18,450 --> 00:00:21,570 if we don't want anything to happen. And this will evaluate. 8 00:00:21,580 --> 00:00:23,310 Python will say, "Yep, this works. 9 00:00:23,370 --> 00:00:26,130 So I'm going to execute whatever code is indented in here". 10 00:00:26,240 --> 00:00:29,570 But what if we want to look up two things at the same time? 11 00:00:29,700 --> 00:00:34,900 What we could do, 'course = "Python for Everybody"', 12 00:00:34,920 --> 00:00:41,510 and we could also set the 'student', name '= "Ezra"'. 13 00:00:42,140 --> 00:00:43,550 And so now we can compare these. 14 00:00:43,550 --> 00:00:49,500 We can say 'if course == "Python for Everybody"', 15 00:00:50,500 --> 00:00:54,400 and then we can nest it in here and say 'if student == 16 00:00:54,900 --> 00:00:57,100 "Ezra": print( 17 00:00:58,500 --> 00:01:00,900 "Welcome to p4e, 18 00:01:02,500 --> 00:01:08,700 Ezra")' And it works because 'course' is "Python for Everybody", 19 00:01:09,000 --> 00:01:15,400 'student' is "Ezra". We have now successfully nested an 'if' statement, 20 00:01:15,470 --> 00:01:17,390 but we can actually merge these together. 21 00:01:17,400 --> 00:01:18,680 This is really easy, too. 22 00:01:18,680 --> 00:01:23,940 So if we just pull this up to this line here and write the 23 00:01:23,950 --> 00:01:27,840 word, 'and', so 'if course == "Python for Everybody" 24 00:01:27,840 --> 00:01:32,300 and if student = "Ezra": "Welcome to the course". 25 00:01:32,300 --> 00:01:34,600 I'll just change the text just to prove that this will work. 26 00:01:34,700 --> 00:01:38,600 And yep, I made a syntax error. 27 00:01:38,600 --> 00:01:40,000 And let's run that. There we go. 28 00:01:40,080 --> 00:01:42,470 So you don't need two 'if' statements in there, that's actually, 29 00:01:42,480 --> 00:01:43,580 I'm glad that one came up. 30 00:01:43,670 --> 00:01:47,940 So it's just 'if' your statement, and some other statement, is 31 00:01:47,950 --> 00:01:52,480 True. Now what this is a lot like saying, is saying, 'if True 32 00:01:52,480 --> 00:01:59,800 and True, do a thing, and we can actually 'print("Do a thing")' 33 00:01:59,830 --> 00:02:01,630 just to make sure that this is working. 34 00:02:01,640 --> 00:02:04,870 So this one is True, and this statement here is True, and 35 00:02:04,880 --> 00:02:07,660 that 'and' operator is saying that both of them need to be True. 36 00:02:07,900 --> 00:02:10,360 Now we can actually swap this out entirely. 37 00:02:10,370 --> 00:02:13,090 We can say that only one of them needs to be True. With the 38 00:02:13,100 --> 00:02:14,700 'and' statement they both need to be True. 39 00:02:14,780 --> 00:02:17,750 And you can put as many 'and' statements together as you want. 40 00:02:17,920 --> 00:02:21,580 But if you only wanted one of these to be True, you could 41 00:02:21,590 --> 00:02:24,760 say either the 'course' is "Python for Everybody", or the 'student' 42 00:02:24,770 --> 00:02:26,830 is "Ezra", "Welcome to the course, 43 00:02:26,840 --> 00:02:30,220 Ezra". It's not necessarily what we're looking for anymore, because 44 00:02:30,220 --> 00:02:32,700 the 'student' might not be "Ezra", they just might be in the 45 00:02:32,700 --> 00:02:33,700 Python course. 46 00:02:33,730 --> 00:02:40,990 So say, "You are either in Python for Everybody, or you are 47 00:02:41,170 --> 00:02:44,860 Ezra". Okay. Now let's go ahead and change this. 48 00:02:44,860 --> 00:02:49,200 Let's change this to something that we know is going to evaluate as False. 49 00:02:49,200 --> 00:02:53,320 But Ezra is still going to stay the same, and that still 50 00:02:53,330 --> 00:02:57,180 works. And if we get rid of this, it doesn't work. 51 00:02:57,180 --> 00:03:01,000 And so what this statement is saying is if something is True, 52 00:03:01,020 --> 00:03:04,080 or something else is True, just one of them needs to match. 53 00:03:04,300 --> 00:03:06,700 And I was going to undo that. Rerun that. 54 00:03:07,100 --> 00:03:13,200 Alright, let's check out a more complicated type of 'if' statements in here. 55 00:03:13,240 --> 00:03:16,540 So this 'if' statement is going to look for one of two things 56 00:03:16,550 --> 00:03:17,680 or something else. 57 00:03:17,690 --> 00:03:19,000 Actually, that's wrong. 58 00:03:19,010 --> 00:03:19,960 I explained that wrong. 59 00:03:19,970 --> 00:03:24,350 It's going to look for a True statement and a True statement, 60 00:03:24,360 --> 00:03:26,480 or another True statement. 61 00:03:26,860 --> 00:03:31,100 And so we can actually split these out by wrapping parentheses 62 00:03:31,100 --> 00:03:33,290 around these. So this is going to get a little bit wild, 63 00:03:33,300 --> 00:03:34,790 and I'll do my best to explain this, 64 00:03:34,800 --> 00:03:37,430 and hopefully this turns out pretty well in the video. 65 00:03:37,430 --> 00:03:41,400 So we've got 'course', 'student', and let's put a 'grade' in here. 66 00:03:42,200 --> 00:03:46,300 So let's just say the 'grade' is going to be, I don't know, grade 12. 67 00:03:46,360 --> 00:03:52,840 So now we can say 'if', with parentheses in here 'student 68 00:03:53,020 --> 00:03:59,920 == "Ezra" and 'course == "Python for everybody") 69 00:04:00,200 --> 00:04:06,400 or grade == 12: print("Success")'. 70 00:04:06,440 --> 00:04:08,600 And sure enough, this is going to show up. 71 00:04:08,610 --> 00:04:12,600 Now, that's because all of these are True. This is True and 72 00:04:12,600 --> 00:04:13,700 this is True. 73 00:04:13,740 --> 00:04:16,560 And because these are together in parentheses, they both 74 00:04:16,570 --> 00:04:17,459 have to be True. 75 00:04:17,470 --> 00:04:21,180 If one fails, this whole thing fails, everything inside the 76 00:04:21,190 --> 00:04:21,959 parentheses fails. 77 00:04:21,959 --> 00:04:23,100 That comes back as False. 78 00:04:23,100 --> 00:04:28,000 And even if that is False, it's going to say, 'or grade == 12'. 79 00:04:28,000 --> 00:04:29,900 So this is a lot like saying, 80 00:04:29,900 --> 00:04:31,900 [no audio] 81 00:04:31,900 --> 00:04:32,900 'if (True 82 00:04:32,900 --> 00:04:35,100 [no audio] 83 00:04:35,100 --> 00:04:39,100 and True) or, 'grade 84 00:04:39,100 --> 00:04:40,500 == 12', True. 85 00:04:40,580 --> 00:04:41,750 So here's the thing. 86 00:04:41,750 --> 00:04:43,200 One of these can fail. 87 00:04:43,200 --> 00:04:45,200 [no audio] 88 00:04:45,200 --> 00:04:48,100 If there is a True and False, this one, it's gone. 89 00:04:49,300 --> 00:04:50,700 Python says, "Get rid of it, 90 00:04:50,880 --> 00:04:53,610 but let's see if there is an or statement in there". 91 00:04:53,620 --> 00:04:55,620 And if there was an 'or' statement, which was True, this is 92 00:04:55,630 --> 00:04:57,880 going to boil down to 'print("Success")'. 93 00:04:57,890 --> 00:05:02,340 So let's just undo that, and change a few variables in here. 94 00:05:02,500 --> 00:05:04,900 Let's change "Ezra" to nothing, "". 95 00:05:04,900 --> 00:05:06,760 So we know that this is going to be False. 96 00:05:08,100 --> 00:05:10,500 This one here is going to be False, but this is still going 97 00:05:10,520 --> 00:05:13,040 to be True, and 'grade == 12', is going to be 'True'. 98 00:05:14,200 --> 00:05:18,000 So let's rerun each of these cells, still works. 99 00:05:18,080 --> 00:05:22,100 'If grade = 11', let's go ahead and change. 100 00:05:22,110 --> 00:05:23,790 This no longer works. 101 00:05:23,800 --> 00:05:24,990 Nothing shows up in there. 102 00:05:25,000 --> 00:05:28,800 And that's because one of these was False, and this one was 103 00:05:28,810 --> 00:05:33,460 False. So neither this one nor this grouped 'if' statement was 104 00:05:33,470 --> 00:05:39,080 True, and because nothing in there was True, when it came to 105 00:05:39,090 --> 00:05:42,380 this 'or' statement, this code was not executed. 106 00:05:42,390 --> 00:05:44,240 Now that's a little more complicated. 107 00:05:44,240 --> 00:05:46,800 You don't really have to fully understand how that works right now 108 00:05:46,860 --> 00:05:49,190 If you're brand new to programming, that's going to be a 109 00:05:49,250 --> 00:05:50,270 little bit confusing. 110 00:05:50,280 --> 00:05:52,490 But you are going to see things like this in the wild. 111 00:05:52,500 --> 00:05:55,040 But over time it does get a little bit easier to understand 112 00:05:55,050 --> 00:05:57,740 this. Now when these get really, really, really big, that 113 00:05:57,750 --> 00:05:58,880 gets very complicated. 114 00:05:58,890 --> 00:06:02,940 And we use a lot more nesting with just simple parentheses. 115 00:06:02,950 --> 00:06:04,380 So just go ahead, 116 00:06:04,380 --> 00:06:05,820 and there we go. 117 00:06:05,830 --> 00:06:07,800 I just undid my work there. 118 00:06:07,940 --> 00:06:10,760 So that's how we work with multiple comparison operators 119 00:06:10,770 --> 00:06:14,660 when it comes to the 'or' statement, or the 'and' statement, we 120 00:06:14,670 --> 00:06:15,500 can do either one. 121 00:06:15,510 --> 00:06:17,870 And in here we can group these together to make sure that 122 00:06:17,880 --> 00:06:19,250 both of these need to be True. 123 00:06:19,250 --> 00:06:23,000 If one of them is not True, this whole thing results in False, 124 00:06:23,020 --> 00:06:26,440 and then it looks for 'if grade == 12', 'if grade == 12', 125 00:06:26,450 --> 00:06:27,400 then it would be True. 126 00:06:27,400 --> 00:06:29,300 But actually, it would look like that. 127 00:06:29,300 --> 00:06:32,000 Now it's saying 'if False or True', 128 00:06:32,000 --> 00:06:35,800 and if we just run this in our interpreter, we can say True 129 00:06:35,800 --> 00:06:38,500 or False, comes back as True. 130 00:06:38,540 --> 00:06:42,150 False or true, also comes back as True, 131 00:06:42,150 --> 00:06:44,250 because True is in there with an 'or' statement 132 00:06:44,250 --> 00:06:45,400 it always comes back. 133 00:06:45,400 --> 00:06:48,150 We can do the opposite as well with an 'and' statement. 134 00:06:48,160 --> 00:06:51,570 So 'True and False', one of them is not True, 135 00:06:51,630 --> 00:06:52,770 it turns out False. 136 00:06:52,940 --> 00:06:55,130 We can do the opposite as well. 137 00:06:55,130 --> 00:06:57,200 False and True, again, 138 00:06:57,260 --> 00:06:58,370 one of those is False, 139 00:06:58,380 --> 00:06:59,750 it's going to return False. 140 00:06:59,750 --> 00:07:02,600 It's always looking for Tuue all the way through. 141 00:07:02,600 --> 00:07:04,400 Hey, that rhymed. I'm going to start using that. 142 00:07:04,460 --> 00:07:06,280 It's always looking for True all the way through. 143 00:07:06,280 --> 00:07:08,040 So I'm going to undo that. 144 00:07:08,050 --> 00:07:11,100 And that is multiple comparison operators. 145 00:07:11,110 --> 00:07:13,470 So when you're learning this honestly, just start small. 146 00:07:13,620 --> 00:07:17,850 Say if one thing is something and another thing is something, 147 00:07:17,850 --> 00:07:20,460 and then you can make it a little more complicated. 148 00:07:20,470 --> 00:07:23,100 And you can say if something is something and something else 149 00:07:23,110 --> 00:07:24,180 is not something. 150 00:07:24,360 --> 00:07:28,050 But for now, like always, take it slow, take it easy, 151 00:07:28,050 --> 00:07:30,270 and over time, this just becomes second nature.