1 00:00:00,550 --> 00:00:02,737 Now that we can do some boolean reasoning, 2 00:00:02,737 --> 00:00:05,731 we should look at how to use it to our advantage in scripts. 3 00:00:05,731 --> 00:00:07,370 We make choices all the time. 4 00:00:07,370 --> 00:00:10,079 If it's raining outside, I'm gonna get an umbrella. 5 00:00:10,079 --> 00:00:12,284 If I'm hungry, I'm gonna eat some food. 6 00:00:12,284 --> 00:00:14,758 If it's late and I'm tired, I go to bed. 7 00:00:14,758 --> 00:00:18,075 Well, unless I'm stuck on a coding problem that I just have to fix. 8 00:00:18,075 --> 00:00:19,318 I should just go to bed at that point. 9 00:00:19,318 --> 00:00:21,973 The answer usually comes in my sleep. 10 00:00:21,973 --> 00:00:24,648 All those scenarios start with the word if. 11 00:00:24,648 --> 00:00:28,887 And that is the first way that we are gonna control the flow of our application. 12 00:00:28,887 --> 00:00:33,199 If you want to learn more, keep watching this video. 13 00:00:33,199 --> 00:00:36,905 In our script here, we ask for a name, we say hello, 14 00:00:36,905 --> 00:00:40,208 and then we say the person is learning Python. 15 00:00:40,208 --> 00:00:43,031 That's not always necessarily true, is it? 16 00:00:43,031 --> 00:00:45,156 So let's do this to fix it. 17 00:00:45,156 --> 00:00:50,427 If the name inputted is actually your name, then we'll print that message. 18 00:00:50,427 --> 00:00:54,107 So in order to do that, we're going to need to check and 19 00:00:54,107 --> 00:00:56,677 see if two values are actually equal. 20 00:00:56,677 --> 00:00:59,991 You can compare two objects using the double equal sign. 21 00:00:59,991 --> 00:01:02,590 Let's take a look real quick in the show. 22 00:01:04,039 --> 00:01:05,252 So here's an example. 23 00:01:05,252 --> 00:01:09,338 We'll say fruit = "apples". 24 00:01:09,338 --> 00:01:13,559 And so we've assigned apples to fruit with a single equal sign. 25 00:01:13,559 --> 00:01:17,486 And then we;ll check a quality by using the double equal signs. 26 00:01:17,486 --> 00:01:21,734 We'll say fruit does that equal apples? 27 00:01:24,196 --> 00:01:24,864 True. 28 00:01:24,864 --> 00:01:28,167 And then if we compare apples to oranges 29 00:01:28,167 --> 00:01:32,778 which you're not supposed to do, you get back False. 30 00:01:32,778 --> 00:01:38,333 And that's because fruit is apples and apples do not equal oranges. 31 00:01:38,333 --> 00:01:41,792 So, note how the single equal sign is used for 32 00:01:41,792 --> 00:01:46,539 assignment and the double equal sign is used for comparison. 33 00:01:46,539 --> 00:01:52,027 Accidental assigning by using just a single equals is a super common problem. 34 00:01:52,027 --> 00:01:54,989 And that's probably because when we say, 35 00:01:54,989 --> 00:01:59,775 fruit equals oranges in English, not fruit equals equals oranges. 36 00:01:59,775 --> 00:02:05,165 So when comparing make sure that you double check for double equals. 37 00:02:05,165 --> 00:02:11,027 So in our code we want to only show the learning Python bid if it's us. 38 00:02:11,027 --> 00:02:13,553 So I'm gonna add a new line here. 39 00:02:15,023 --> 00:02:20,570 And start it with a new keyword that you haven't seen yet and it's called if. 40 00:02:20,570 --> 00:02:24,782 So if, then what follows next is an expression. 41 00:02:24,782 --> 00:02:26,386 So let's see. 42 00:02:26,386 --> 00:02:31,888 We have first name already, so if first name is equal to, 43 00:02:31,888 --> 00:02:34,869 note the double equals there, 44 00:02:34,869 --> 00:02:40,120 Craig, Double-check the double equals always. 45 00:02:40,120 --> 00:02:44,586 And then, we add a colon, which is a new character for us. 46 00:02:44,586 --> 00:02:49,667 What happens is the colon opens up the body of our if statement. 47 00:02:49,667 --> 00:02:54,513 Now, the body is what will run if this expression is true. 48 00:02:54,513 --> 00:02:59,281 The way that you group the code together that will run when the expression is true 49 00:02:59,281 --> 00:03:00,929 is by indenting each line. 50 00:03:00,929 --> 00:03:05,335 Now, you can indent your code with a tab or a series of spaces. 51 00:03:05,335 --> 00:03:10,151 The only requirement is that all indentation in your file is exactly 52 00:03:10,151 --> 00:03:10,916 the same. 53 00:03:10,916 --> 00:03:13,796 As you can imagine, that might cause some problems. 54 00:03:13,796 --> 00:03:17,198 So in order to standardize that decision for you, 55 00:03:17,198 --> 00:03:22,467 there is a very strong movement to use forespaces to indent your Python code. 56 00:03:22,467 --> 00:03:24,712 So as you can see down here in the editor, 57 00:03:24,712 --> 00:03:27,564 this says we're gonna use spaces instead of tabs. 58 00:03:27,564 --> 00:03:33,863 So we're gonna use spaces and then this 2 here is saying the number of spaces. 59 00:03:33,863 --> 00:03:35,757 And I just said that we wanna use four. 60 00:03:35,757 --> 00:03:40,197 So I'm gonna change the default to be 4. 61 00:03:40,197 --> 00:03:44,306 Four spaces is the default in most text editors for Python. 62 00:03:44,306 --> 00:03:46,385 And now we've just changed ours. 63 00:03:46,385 --> 00:03:48,225 More in the teacher's notes. 64 00:03:48,225 --> 00:03:53,087 So you'll notice that when I press Enter after this if statement, you'll 65 00:03:53,087 --> 00:03:57,961 see that the cursor is automatically intended to where the code should go. 66 00:03:57,961 --> 00:04:01,040 Here you'll see that we are on line 4 which is true. 67 00:04:01,040 --> 00:04:04,650 Line 4 and we're in column 5 which is 4 spaces, right? 68 00:04:04,650 --> 00:04:09,071 There's 4 line spaces and then we start writing, awesome. 69 00:04:09,071 --> 00:04:13,039 But actually what we really want is this line here, right? 70 00:04:13,039 --> 00:04:15,403 We want this line here up there. 71 00:04:15,403 --> 00:04:18,218 So what I'm gonna do is I'm gonna move to the front of this and 72 00:04:18,218 --> 00:04:19,655 I'm gonna press Backspace and 73 00:04:19,655 --> 00:04:22,881 you'll see that it automatically indented into our if block there. 74 00:04:22,881 --> 00:04:28,577 You can finish this grouping or block of code as we call it, block. 75 00:04:28,577 --> 00:04:32,552 You can end that by going back to the original indentation level. 76 00:04:32,552 --> 00:04:35,138 So if we come to the end here and we press Enter. 77 00:04:35,138 --> 00:04:36,450 It's waiting for us there. 78 00:04:36,450 --> 00:04:39,096 I can press Backspace back here. 79 00:04:39,096 --> 00:04:41,720 So let's just add a new closing line so that we can see it. 80 00:04:41,720 --> 00:04:46,982 So we'll say print, we'll say ("Have a great day. 81 00:04:46,982 --> 00:04:48,831 Let's use our format string. 82 00:04:48,831 --> 00:04:51,406 We'll use the name there one more time. 83 00:04:51,406 --> 00:04:55,630 And we'll say format, and we'll pass in first_name. 84 00:04:55,630 --> 00:04:58,996 Do you see how the indentation should stops right, 85 00:04:58,996 --> 00:05:02,840 it shows that this is clearly not part of the if statement. 86 00:05:02,840 --> 00:05:04,580 So let's run it and see if it's working. 87 00:05:04,580 --> 00:05:08,497 I'm gonna drop out of here, I'm gonna make sure that this is saved, 88 00:05:08,497 --> 00:05:11,618 there's no red mark up there, we'll say clear here. 89 00:05:11,618 --> 00:05:15,243 Let's go ahead and say python hello.py. 90 00:05:17,325 --> 00:05:18,629 We've got an error. 91 00:05:18,629 --> 00:05:20,570 Let's see what's happening. 92 00:05:20,570 --> 00:05:22,114 Did you spot it? 93 00:05:22,114 --> 00:05:24,373 Unexpected end of file while parsing. 94 00:05:24,373 --> 00:05:27,111 So, here we go, look what I forgot. 95 00:05:27,111 --> 00:05:29,986 I forgot the final paren there. 96 00:05:29,986 --> 00:05:30,716 So there we go. 97 00:05:30,716 --> 00:05:32,423 So it was just waiting for us to finish. 98 00:05:32,423 --> 00:05:34,667 That's awesome, that's exactly the error I showed. 99 00:05:34,667 --> 00:05:35,638 See, it happens. 100 00:05:35,638 --> 00:05:37,214 Here we go. 101 00:05:37,214 --> 00:05:40,167 What is your first name. 102 00:05:40,167 --> 00:05:42,093 My name is Craig. 103 00:05:42,093 --> 00:05:45,197 And so there we see, Craig is learning Python, 104 00:05:45,197 --> 00:05:48,387 because it went in to the if block and checked it. 105 00:05:48,387 --> 00:05:50,364 And now let's test the other side of it. 106 00:05:50,364 --> 00:05:54,690 Let's run it again without the name Craig or without your name. 107 00:05:54,690 --> 00:05:57,065 So how about my buddy Kenneth? 108 00:05:57,065 --> 00:05:59,146 He's a great teacher and Pythonista and 109 00:05:59,146 --> 00:06:01,541 you'll meet him in a future course I'm sure. 110 00:06:01,541 --> 00:06:02,339 Hello Kenneth. 111 00:06:02,339 --> 00:06:04,315 Have a great day Kenneth. 112 00:06:04,315 --> 00:06:06,165 So there we go. We don't have the output Kenneth 113 00:06:06,165 --> 00:06:08,861 is learning Python because our if condition here, right? 114 00:06:08,861 --> 00:06:13,838 This condition first name equals Craig Kenneth does not equal Craig, so 115 00:06:13,838 --> 00:06:14,986 that was false. 116 00:06:14,986 --> 00:06:16,135 You know what though, 117 00:06:16,135 --> 00:06:20,686 I'm kinda feeling like everybody should probably start learning Python, don't you? 118 00:06:20,686 --> 00:06:26,511 So how about everybody that isn't you gets a message about learning Python. 119 00:06:26,511 --> 00:06:29,574 And this way you could run it for your family or friends, and 120 00:06:29,574 --> 00:06:33,016 give them a little motivation, a little wink wink, nudge nudge. 121 00:06:33,016 --> 00:06:37,787 So you can very easily do this, and the key word is Else. 122 00:06:37,787 --> 00:06:43,696 So at the same indentation level as your if, use the keyword else and 123 00:06:43,696 --> 00:06:47,647 then a colon because elses also have bodies. 124 00:06:47,647 --> 00:06:51,916 So the next body of code and see how this indented automatically there? 125 00:06:51,916 --> 00:06:57,424 We'll say print("You should 126 00:06:57,424 --> 00:07:03,136 totally learn Python, {}!. 127 00:07:03,136 --> 00:07:07,520 Go ahead and close that string, format, push in our first name and 128 00:07:07,520 --> 00:07:11,765 then this time I'm gonna remember to close my print statement. 129 00:07:11,765 --> 00:07:13,063 There we go. 130 00:07:13,063 --> 00:07:17,329 So what happens is, we come in here, we check the first_name, 131 00:07:17,329 --> 00:07:21,286 if this is true, this runs, otherwise, this block runs. 132 00:07:24,244 --> 00:07:28,214 Do you see how the indentation really just makes the code kinda jut out? 133 00:07:28,214 --> 00:07:32,168 It's kinda like branches of a tree, this is why it's called branching. 134 00:07:32,168 --> 00:07:36,946 Now, this spacing that you see is also referred to as white space. 135 00:07:36,946 --> 00:07:41,316 One thing to do before we verify that this is working is to talk about our final 136 00:07:41,316 --> 00:07:42,561 branching keyword. 137 00:07:42,561 --> 00:07:47,421 It's one of the only Python keywords that isn't actually a word in English, 138 00:07:47,421 --> 00:07:51,775 it's called elif, and it's a combination of the words else and if. 139 00:07:51,775 --> 00:07:57,083 And what this allows you to do is chain different conditions, so let's do this. 140 00:07:57,083 --> 00:08:00,172 Now I told you before that you're not alone in your journey. 141 00:08:00,172 --> 00:08:02,803 Your fellow students are taking this course right now. 142 00:08:02,803 --> 00:08:05,515 So let's go to the community and get some names. 143 00:08:05,515 --> 00:08:07,066 So here I am on the Treehouse site, 144 00:08:07,066 --> 00:08:10,770 it's most likely gonna look different cuz we're constantly improving things. 145 00:08:10,770 --> 00:08:15,185 But up here in the header, there is a link to the Community link. 146 00:08:15,185 --> 00:08:20,262 And this by default is all of the topics, but we can narrow that. 147 00:08:20,262 --> 00:08:22,368 So let's narrow that down here to Python. 148 00:08:25,742 --> 00:08:27,699 Let's take a look at this first one here. 149 00:08:27,699 --> 00:08:30,693 Is there a reason we are not setting the condition in the while statement? 150 00:08:30,693 --> 00:08:33,451 We're not quite there yet, but we're going to see that here in a second. 151 00:08:33,451 --> 00:08:35,477 So let's go here. 152 00:08:35,477 --> 00:08:38,188 Let's take a look at this, and this is Maximilliane. 153 00:08:38,188 --> 00:08:40,926 Let's grab her name here. 154 00:08:40,926 --> 00:08:45,649 Let's say Maximilliane Quel, if I can, Maximiliane. 155 00:08:45,649 --> 00:08:46,855 So I'm gonna copy that. 156 00:08:46,855 --> 00:08:50,075 And you'll see here, she's gone and she's asked and 157 00:08:50,075 --> 00:08:52,182 she's put some questions in here. 158 00:08:52,182 --> 00:08:53,346 She got an answer back. 159 00:08:53,346 --> 00:08:55,437 And there's an answer there from Steven Parker. 160 00:08:55,437 --> 00:08:58,762 And she was told good job on what it was that she was asking. 161 00:08:58,762 --> 00:09:00,986 We'll get to this here just in a bit. 162 00:09:00,986 --> 00:09:06,097 So I'm gonna pop back over and we're gonna use Maximiliane in our example here. 163 00:09:06,097 --> 00:09:11,512 So if it's me, we wanna see this is learning Python. 164 00:09:11,512 --> 00:09:17,058 But if it's specifically Maximiliane, cuz we know she's in there, 165 00:09:17,058 --> 00:09:24,113 we'll say if first_name =, and I hope I'm saying her name right, "Maximiliane". 166 00:09:24,113 --> 00:09:27,735 Then we'll come in here and 167 00:09:27,735 --> 00:09:31,987 will say print (first_name, 168 00:09:31,987 --> 00:09:39,670 "is learning with fellow students in the Community! 169 00:09:39,670 --> 00:09:42,620 Me too!"). 170 00:09:42,620 --> 00:09:45,124 And there we go, we added another branch. 171 00:09:45,124 --> 00:09:48,383 So let's go ahead and let's run it for Maximiliane. 172 00:09:48,383 --> 00:09:53,192 It's gonna be, First 173 00:09:53,192 --> 00:09:58,224 name is M-A-X-I-M-I, Maximiliane. 174 00:09:58,224 --> 00:10:00,567 Maximiliane is learning with fellow students in the community. 175 00:10:00,567 --> 00:10:02,076 Me too. 176 00:10:02,076 --> 00:10:04,862 Cool, that's awesome that you're gonna hang out there. 177 00:10:04,862 --> 00:10:07,429 Helping others is a wonderful way to make sure your learning sticks. 178 00:10:07,429 --> 00:10:09,043 I'm so proud of you. 179 00:10:09,043 --> 00:10:13,591 So looking at the code, we see that it checks this if statement, 180 00:10:13,591 --> 00:10:15,880 is Maximiliane equal to Craig? 181 00:10:15,880 --> 00:10:16,943 That is not true. 182 00:10:16,943 --> 00:10:20,380 Elsif Maximiliane is equal to Maximiliane print this. 183 00:10:20,380 --> 00:10:23,487 And because that happened, we're not gonna hit this otherwise block. 184 00:10:23,487 --> 00:10:25,933 So let's run one that does hit the otherwise block. 185 00:10:25,933 --> 00:10:26,622 Let's do that real quick. 186 00:10:26,622 --> 00:10:32,148 We'll say Python, and let's use one of our JavaScript teachers here, Treasure. 187 00:10:35,492 --> 00:10:39,603 And there it says, hello, Treasure, you should totally learn Python, Treasure! 188 00:10:39,603 --> 00:10:42,835 I think she's learning Python actually, she's kind of a polyglot, 189 00:10:42,835 --> 00:10:45,972 she talks a whole bunch of these different programming languages. 190 00:10:45,972 --> 00:10:50,461 So that equality comparison operative, the double equals, 191 00:10:50,461 --> 00:10:54,531 is just one of many ways that we can compare our objects. 192 00:10:54,531 --> 00:10:59,410 So if you would like to learn more about other comparison operators 193 00:10:59,410 --> 00:11:01,769 then watch the upcoming video. 194 00:11:01,769 --> 00:11:05,999 Elsif you are feeling like you want to check out the community forum then you 195 00:11:05,999 --> 00:11:10,365 totally should, else you should take a break and then come back refreshed and 196 00:11:10,365 --> 00:11:11,860 ready to compare things.