1 00:00:00,000 --> 00:00:03,360 If by now you've been following through the last few videos 2 00:00:03,420 --> 00:00:07,170 of basically learning about comparison operators and multiple 3 00:00:07,180 --> 00:00:10,050 comparison operators together, you might have already run 4 00:00:10,050 --> 00:00:15,100 into a particular issue where you could say, 'name = "Kalob"', 5 00:00:15,120 --> 00:00:17,730 and then what do you do if you're looking for a specific 6 00:00:17,740 --> 00:00:20,600 'name', or what do you do if you're looking for a specific 7 00:00:20,600 --> 00:00:22,300 'name', and nothing else matches? 8 00:00:22,340 --> 00:00:27,230 So right now, we could do 'if name == "Kalob": print( 9 00:00:27,900 --> 00:00:29,700 "Name is Kalob")' 10 00:00:29,740 --> 00:00:31,780 But what if the 'name' is not "Kalob"? 11 00:00:31,780 --> 00:00:39,300 Then we would say, 'if name == "Henry": print("Name is Henry")'. 12 00:00:39,380 --> 00:00:41,690 But what if 'name' is not "Kalob" or "Henry"? 13 00:00:42,200 --> 00:00:49,000 Then we could say, "if name == "Zephyr": print("Name is Zephyr")'. 14 00:00:49,000 --> 00:00:51,100 And let's run that. It says, "Name is Kalob". 15 00:00:51,100 --> 00:00:54,100 And if it is "Zephyr", then cool, 16 00:00:54,100 --> 00:00:56,100 it's going to show up as "Name is Zephyr". 17 00:00:57,800 --> 00:00:59,400 But what if it's none of those names? 18 00:00:59,400 --> 00:01:01,200 What if the name is "Ezra"? 19 00:01:01,800 --> 00:01:02,800 Nothing happens. 20 00:01:02,800 --> 00:01:04,650 What if we wanted to catch that, 21 00:01:04,860 --> 00:01:08,490 or what if we want our program to not have to evaluate every 22 00:01:08,500 --> 00:01:09,570 single one of these lines? 23 00:01:09,570 --> 00:01:11,100 We just want it to match one. 24 00:01:11,100 --> 00:01:15,700 So if the 'name = "Henry"', well, it should only match this one, 25 00:01:15,780 --> 00:01:18,240 but still it's going to try to match this one, 26 00:01:18,250 --> 00:01:19,590 it's going to try to match this one, 27 00:01:19,600 --> 00:01:22,000 and even if it does find a match, it's going to find 28 00:01:22,000 --> 00:01:24,930 this ones. We've actually run into two problems here, where it's 29 00:01:24,940 --> 00:01:27,840 going to continue to execute these 'if' statements, even though 30 00:01:27,850 --> 00:01:30,090 they are sort of related, and we don't want them to be. 31 00:01:30,300 --> 00:01:34,800 And if it does not match "Kalob", "Henry", or "Zephyr", what do we do? 32 00:01:34,800 --> 00:01:38,100 So, I'll just rerun those, and we'll say the 'name = "Henry"'. 33 00:01:38,150 --> 00:01:41,300 Now, when we get two chaining operators together, we have 34 00:01:41,300 --> 00:01:45,200 these things called 'if-else' statements, or 'else-if' statements. 35 00:01:45,200 --> 00:01:52,100 So we can say 'if name == "Kalob": print("Kalob")'. 36 00:01:52,900 --> 00:01:59,100 We can also say 'elif name == "Henry": print("Meow")', 37 00:01:59,100 --> 00:02:00,300 because that's one of my cats. 38 00:02:01,800 --> 00:02:09,600 'elif name == "Zephyr": print("Little meow")', because 39 00:02:09,600 --> 00:02:11,199 he has a tiny little meow. 40 00:02:11,199 --> 00:02:16,000 He's got a tiny voice. And we're going to see that it just says "Meow". 41 00:02:16,000 --> 00:02:18,500 Now, the nice thing about this is Python is going to try 42 00:02:18,500 --> 00:02:20,500 to evaluate this and says, "You know what, 43 00:02:20,560 --> 00:02:22,360 the name was not Kalob, 44 00:02:22,360 --> 00:02:24,200 the name is Henry", as we can see here. 45 00:02:24,200 --> 00:02:27,400 Okay, so it's going to execute that one, and then it gets to the 'elif', 46 00:02:27,470 --> 00:02:31,640 and it says, by the way, 'elif' is short for 'else-if' in Python. 47 00:02:31,760 --> 00:02:35,830 So the 'elif' is saying, "If the name is then equal to "Henry", 48 00:02:35,900 --> 00:02:38,000 print me out". Now, this turned out to be True. 49 00:02:38,000 --> 00:02:39,500 The name was "Henry". 50 00:02:39,560 --> 00:02:42,920 And so Python said, "Oh, okay, the name was Henry. 51 00:02:43,180 --> 00:02:45,160 I do not need to execute this. 52 00:02:45,170 --> 00:02:46,150 I don't need to check it". 53 00:02:46,160 --> 00:02:49,300 Now, Python is very fast when it comes to checking 'if' statements. 54 00:02:49,310 --> 00:02:52,120 So like, you're not going to get massive performance boosts, 55 00:02:52,390 --> 00:02:55,210 but when you are writing code that is connected, this is 56 00:02:55,220 --> 00:02:59,380 a lot cleaner, because now if I wrote this and another Dev 57 00:02:59,380 --> 00:03:01,240 came in and said, "Oh, what is this looking for?" 58 00:03:01,250 --> 00:03:04,300 They could say it's looking for "Kalob", or it's looking for 59 00:03:04,310 --> 00:03:05,680 "Henry", or it's looking for "Zephyr". 60 00:03:05,780 --> 00:03:06,890 We're up here. 61 00:03:06,900 --> 00:03:09,320 It's looking for 'name' is "Kalob", 62 00:03:09,350 --> 00:03:10,730 'name' could also be "Henry", 63 00:03:10,740 --> 00:03:12,290 'name' could also be "Zephyr". 64 00:03:12,300 --> 00:03:15,200 There is a little bit of a syntactical difference there, 65 00:03:15,200 --> 00:03:18,260 but it is also important just for reading the code. 66 00:03:18,500 --> 00:03:25,200 Now, what happens if the name was not "Kalob", "Henry", or "Zephyr"? 67 00:03:25,210 --> 00:03:26,790 What if it was any other name? 68 00:03:27,100 --> 00:03:31,720 What we could add in here, an 'else' statement, and an 'else' 69 00:03:31,730 --> 00:03:34,090 statement does not take a comparison operator. 70 00:03:34,100 --> 00:03:38,170 It just says if it is something cool, if it's not, if it 71 00:03:38,180 --> 00:03:42,640 is literally anything else, execute this code. It will say, 72 00:03:42,640 --> 00:03:44,200 print in here with an 'f""' statement. 73 00:03:44,200 --> 00:03:46,400 'print(f" The name is ")'. 74 00:03:46,400 --> 00:03:49,300 Now this still result in "Meow", because I have to go and change that 75 00:03:49,340 --> 00:03:51,670 'name', and I'm not going to change it up here because I 76 00:03:51,670 --> 00:03:54,900 like this example, but I am going to overwrite it right here. 77 00:03:54,900 --> 00:03:57,400 'name = "Ted"'. 78 00:03:57,400 --> 00:03:59,000 And let's go ahead and run this. 79 00:03:59,200 --> 00:04:00,700 It says, "The name is Ted". 80 00:04:00,700 --> 00:04:02,800 [no audio] 81 00:04:02,800 --> 00:04:07,900 "Kalob". Yep, shows up as, "Kalob". "Zephyr", it's "Little meow" because he's got 82 00:04:07,900 --> 00:04:16,100 a little meow or 'name' is "Literally anything else in the entire 83 00:04:16,100 --> 00:04:19,700 world". That does not match "Kalob", "Henry", or "Zephyr". 84 00:04:20,010 --> 00:04:21,149 Just go ahead and run this. 85 00:04:21,200 --> 00:04:24,600 And it says, "The name is Literally anything else in the entire world". 86 00:04:24,640 --> 00:04:27,940 Now, you don't actually need these 'elif' statements either. 87 00:04:28,000 --> 00:04:30,700 You could say 'name =', let's not do 'name', 88 00:04:30,700 --> 00:04:34,200 let's do 'age = 25', 89 00:04:34,200 --> 00:04:39,500 and you could say, 'if age < 21': 90 00:04:39,500 --> 00:04:45,900 print("Nope, not allowed to have alcohols")'. 91 00:04:45,900 --> 00:04:51,300 Otherwise, if you are older than 21, 'print("Welcome to the bar, 92 00:04:51,300 --> 00:04:53,800 what are you drinking tonight?")' 93 00:04:53,800 --> 00:04:55,900 [no audio] 94 00:04:55,900 --> 00:04:59,700 And just like that, Python is going to say, "Okay, the age is 25. 95 00:04:59,700 --> 00:05:04,100 if age", which looks like this, "if 25 < 21", 96 00:05:04,100 --> 00:05:05,200 Nope, it is not. 97 00:05:05,260 --> 00:05:07,660 So it's not going to execute this. 98 00:05:07,670 --> 00:05:09,280 It just says, "I'm going to get here. 99 00:05:09,280 --> 00:05:10,900 Nope. That's False. 100 00:05:10,900 --> 00:05:15,400 If False, no". Comparison operator is always looking for True. 101 00:05:15,480 --> 00:05:19,410 So it skips over this and then goes straight to the 'else' statement, 102 00:05:19,410 --> 00:05:22,740 and the 'else' statement says, if it is literally any other answer, 103 00:05:22,750 --> 00:05:26,010 doesn't matter what the answer is, it is the opposite of 104 00:05:26,020 --> 00:05:30,640 this. And so to catch any sort of special cases, we use an 105 00:05:30,650 --> 00:05:31,720 'elif' statement. 106 00:05:32,200 --> 00:05:34,420 But if you're just looking for something simple, like if 107 00:05:34,450 --> 00:05:37,150 you're allowed to drink alcohol or if you're allowed to vote, 108 00:05:37,160 --> 00:05:39,740 all you need is an 'if-else' statement in here. 109 00:05:39,740 --> 00:05:42,600 Now you're going to be writing a lot of these 110 00:05:42,660 --> 00:05:46,240 in, well every programming language. Python is no different in that 111 00:05:46,240 --> 00:05:50,300 case, just because this is how you tell a program what to do. 112 00:05:50,300 --> 00:05:53,300 If something is something, do something, if it is not 113 00:05:53,300 --> 00:05:54,500 do something else. 114 00:05:54,540 --> 00:05:58,950 And programs are made up of hundreds, thousands, millions, 115 00:05:58,960 --> 00:06:01,680 maybe even billions of 'if' statements, depending how big the 116 00:06:02,280 --> 00:06:03,360 program is. 117 00:06:03,370 --> 00:06:06,570 Google probably has a good example of billions of 'if' statements 118 00:06:06,700 --> 00:06:08,650 in all of their source code. 119 00:06:08,660 --> 00:06:13,430 But what I'd like you to do as quick little hands on example, 120 00:06:13,430 --> 00:06:17,510 is I want you to set a number, check if that number is a certain 121 00:06:17,520 --> 00:06:20,080 number. So I'm going to let you write your own 'if' statement 122 00:06:20,090 --> 00:06:22,240 here, and also write an 'else' statement, 123 00:06:22,240 --> 00:06:25,660 and then I want you to change your variable number so that 124 00:06:25,670 --> 00:06:28,870 the first statement is triggered, and then the second statement 125 00:06:28,880 --> 00:06:30,800 is triggered, but not at the same time. 126 00:06:30,810 --> 00:06:33,760 So one should run, and then you change your variable, and 127 00:06:33,770 --> 00:06:35,530 the other one should then run. 128 00:06:36,220 --> 00:06:37,600 Go ahead and give that a shot, 129 00:06:37,610 --> 00:06:40,600 and when you're done let's head on over to that next lesson.