1 00:00:00,820 --> 00:00:02,600 - [Narrator] In this video, we're going to talk a 2 00:00:02,600 --> 00:00:05,200 little bit more about the if statement 3 00:00:05,200 --> 00:00:09,510 before we begin looking at its alternative versions. 4 00:00:09,510 --> 00:00:12,930 Now, back in the preceding lesson we demonstrated 5 00:00:12,930 --> 00:00:15,230 the basic functionality of the if statement. 6 00:00:15,230 --> 00:00:19,050 Let's do that again here by first declaring a variable 7 00:00:19,050 --> 00:00:22,320 called "grade" and we'll give it the value of 85, 8 00:00:22,320 --> 00:00:25,100 and let's write an if statement that's going to check 9 00:00:25,100 --> 00:00:28,980 whether the grade value is greater than or equal to 60, 10 00:00:28,980 --> 00:00:32,313 which when you're taking a course is normally an indication 11 00:00:32,313 --> 00:00:37,313 that you've passed the course, and in the body or the suite 12 00:00:37,570 --> 00:00:39,450 of that if statement, let's go ahead 13 00:00:39,450 --> 00:00:42,950 and simply display the string passed, 14 00:00:42,950 --> 00:00:46,980 if in fact the grade is greater than or equal to 60. 15 00:00:46,980 --> 00:00:50,330 Now, based on what we define the grade to be up above, 16 00:00:50,330 --> 00:00:55,030 this condition will be true and as I execute this statement 17 00:00:55,030 --> 00:00:57,620 it's going to show me the word "passed." 18 00:00:57,620 --> 00:01:00,620 Now, before I continue, I just want to point out here 19 00:01:00,620 --> 00:01:05,620 that when you press enter on an indented suite line 20 00:01:05,820 --> 00:01:09,170 inside of a control statement like the if statement, 21 00:01:09,170 --> 00:01:13,980 it assumes that you'd like to continue defining the body 22 00:01:13,980 --> 00:01:16,340 or the suite of that statement. 23 00:01:16,340 --> 00:01:18,930 So in order to actually execute the code, 24 00:01:18,930 --> 00:01:21,140 I now have to press enter once again, 25 00:01:21,140 --> 00:01:23,210 and at this point I can see 26 00:01:23,210 --> 00:01:25,910 the string "passed" gets displayed. 27 00:01:25,910 --> 00:01:28,670 Now, we started to briefly mention 28 00:01:28,670 --> 00:01:33,670 in the preceding lesson this issue of indentation in Python. 29 00:01:33,800 --> 00:01:36,350 Unlike most C-based languages, 30 00:01:36,350 --> 00:01:39,340 which typically use curly braces, 31 00:01:39,340 --> 00:01:41,800 like the left curly brace and the right curly brace 32 00:01:41,800 --> 00:01:43,230 that I just typed here, 33 00:01:43,230 --> 00:01:46,680 to delimit the body of a control statement 34 00:01:46,680 --> 00:01:50,330 like the if statement, in Python, the suite 35 00:01:50,330 --> 00:01:53,760 of an if statement, and similarly other control statements, 36 00:01:53,760 --> 00:01:57,750 is defined by indentation only. 37 00:01:57,750 --> 00:02:01,440 The default indentation level is four spaces, 38 00:02:01,440 --> 00:02:04,470 and that's what I got here automatically 39 00:02:04,470 --> 00:02:07,610 when I simply pressed enter while typing the code 40 00:02:07,610 --> 00:02:11,670 in the context of the interactive I-Python interpreter. 41 00:02:11,670 --> 00:02:14,750 If you're working in an integrated development environment, 42 00:02:14,750 --> 00:02:17,930 then you'll typically have four spaces by default, 43 00:02:17,930 --> 00:02:20,120 although that is configurable, 44 00:02:20,120 --> 00:02:23,260 and there's nothing that says you have to use four spaces. 45 00:02:23,260 --> 00:02:25,890 What Python does require, however, 46 00:02:25,890 --> 00:02:29,280 is that whatever indentation level you choose, 47 00:02:29,280 --> 00:02:32,210 and whether you choose to use spaces or tabs, 48 00:02:32,210 --> 00:02:34,630 you have to do that uniformly 49 00:02:34,630 --> 00:02:37,920 for everything in a given suite. 50 00:02:37,920 --> 00:02:41,090 So, if, for example, I recalled the statement 51 00:02:41,090 --> 00:02:44,030 that I just did, and, let me go back up to the print line 52 00:02:44,030 --> 00:02:46,633 and get rid of the indentation. 53 00:02:47,540 --> 00:02:50,060 So when you're defining an if statement, 54 00:02:50,060 --> 00:02:53,540 it expects its suite to be indented. 55 00:02:53,540 --> 00:02:56,780 This is actually an error in Python, 56 00:02:56,780 --> 00:02:59,710 and to demonstrate that, let me go ahead and press enter, 57 00:02:59,710 --> 00:03:03,030 and now you can see that I have an indentation error 58 00:03:03,030 --> 00:03:05,030 and it's telling me in this statement 59 00:03:05,030 --> 00:03:08,050 that was not indented properly. 60 00:03:08,050 --> 00:03:11,980 Now, similarly, going back into my if statement here, 61 00:03:11,980 --> 00:03:14,900 let me put the four spaces back in, 62 00:03:14,900 --> 00:03:18,230 if the indentation is not consistent, 63 00:03:18,230 --> 00:03:20,240 that also is a problem. 64 00:03:20,240 --> 00:03:24,310 So, for example, in this first suite statement, 65 00:03:24,310 --> 00:03:26,980 I have four spaces before the print. 66 00:03:26,980 --> 00:03:30,800 On the next line, I've removed two of those spaces, 67 00:03:30,800 --> 00:03:33,347 and let's create another print statement that says, 68 00:03:33,347 --> 00:03:38,347 "Good job!" and if I now go ahead 69 00:03:38,770 --> 00:03:43,140 and try to execute this statement, once again, 70 00:03:43,140 --> 00:03:45,680 I get an indentation error, this time pointing 71 00:03:45,680 --> 00:03:47,870 to the "Good job!" line and the reason 72 00:03:47,870 --> 00:03:51,020 is there are only two spaces in front of print 73 00:03:51,020 --> 00:03:54,380 in this case as opposed to four spaces up above. 74 00:03:54,380 --> 00:03:58,820 So, not only do you have to maintain the consistency 75 00:03:58,820 --> 00:04:02,730 of your spacing, the interpreter is going to check 76 00:04:02,730 --> 00:04:05,380 that spacing for you, and it turns out, 77 00:04:05,380 --> 00:04:09,560 there's also static code analysis tools that can scan 78 00:04:09,560 --> 00:04:13,420 larger code files for things like this, as well. 79 00:04:13,420 --> 00:04:16,410 So, that's the basics of the if statement. 80 00:04:16,410 --> 00:04:18,450 Now, another thing I want to point out 81 00:04:18,450 --> 00:04:20,400 about the if statement is the fact 82 00:04:20,400 --> 00:04:23,110 that the condition can be any expression 83 00:04:23,110 --> 00:04:25,810 that evaluates to true or false, 84 00:04:25,810 --> 00:04:30,450 and it turns out that in Python everything has a value 85 00:04:30,450 --> 00:04:33,080 of true or false associated with it. 86 00:04:33,080 --> 00:04:36,547 So for example, if I go ahead and say, 87 00:04:36,547 --> 00:04:41,020 "If one," any value that's nonzero is going 88 00:04:41,020 --> 00:04:45,710 to be treated as true, and zero is treated as false. 89 00:04:45,710 --> 00:04:47,660 So, if I say, "If one," and I want 90 00:04:47,660 --> 00:04:51,207 to do a print statement here that simply says, 91 00:04:51,207 --> 00:04:56,207 "Nonzero values are true, so this will print." 92 00:05:00,610 --> 00:05:02,970 So we got a little if statement there. 93 00:05:02,970 --> 00:05:06,140 I expect to see the print happen in this case 94 00:05:06,140 --> 00:05:08,300 because one is considered to be true, 95 00:05:08,300 --> 00:05:12,100 so when I execute that, sure enough it does execute. 96 00:05:12,100 --> 00:05:13,830 On the other hand, if I use zero, 97 00:05:13,830 --> 00:05:17,190 which is false by default in Python, 98 00:05:17,190 --> 00:05:20,237 and I do a little print statement and say, 99 00:05:20,237 --> 00:05:25,237 "Zero is false, so this will not print." 100 00:05:29,550 --> 00:05:33,020 You can see that indeed nothing is displayed 101 00:05:33,020 --> 00:05:35,670 as a result of that condition. 102 00:05:35,670 --> 00:05:40,170 Now as we move forward through subsequent lessons, 103 00:05:40,170 --> 00:05:43,410 we're going to see that more complex objects 104 00:05:43,410 --> 00:05:47,540 also can be evaluated as true or false. 105 00:05:47,540 --> 00:05:50,920 So for example, a string that contains characters 106 00:05:50,920 --> 00:05:54,030 would be considered true, and an empty string 107 00:05:54,030 --> 00:05:57,660 that contains no characters would be considered false, 108 00:05:57,660 --> 00:05:59,580 and similarly, when we get into 109 00:05:59,580 --> 00:06:01,820 the various Python data structures, 110 00:06:01,820 --> 00:06:04,560 which we'll start to do here in this lesson, 111 00:06:04,560 --> 00:06:07,160 they, too, can be evaluated that way. 112 00:06:07,160 --> 00:06:10,240 So, a collection of items that has elements in it, 113 00:06:10,240 --> 00:06:14,990 has items in it, like a collection of random integers, 114 00:06:14,990 --> 00:06:19,640 that would be considered true, whereas an empty collection 115 00:06:19,640 --> 00:06:22,460 would generally be considered false. 116 00:06:22,460 --> 00:06:23,640 Now, there's one other thing 117 00:06:23,640 --> 00:06:27,400 that I want to point out about conditions 118 00:06:27,400 --> 00:06:30,620 specifically with the double equals operator. 119 00:06:30,620 --> 00:06:34,730 If we go and write an expression like grade is assigned 85, 120 00:06:34,730 --> 00:06:36,950 which is what we did up above, 121 00:06:36,950 --> 00:06:41,950 that is going to assign the value 85 to the variable grade. 122 00:06:42,610 --> 00:06:45,870 Now, before I execute that let me get rid of that. 123 00:06:45,870 --> 00:06:47,870 Let's assume grade already exists, 124 00:06:47,870 --> 00:06:50,340 which it does up above we defined it. 125 00:06:50,340 --> 00:06:52,627 So if I go and write by accident, 126 00:06:52,627 --> 00:06:57,020 "Grade double equals 85," you can see that I get a result 127 00:06:57,020 --> 00:06:59,540 of true because if I scroll back up here, 128 00:06:59,540 --> 00:07:03,040 you can see that indeed, grade was 85. 129 00:07:03,040 --> 00:07:08,040 So, if I mistakenly accidentally typed two equal signs, 130 00:07:09,040 --> 00:07:11,780 then I would get a boolean value. 131 00:07:11,780 --> 00:07:16,030 Similarly, if I mistakenly was trying to assign a value, 132 00:07:16,030 --> 00:07:18,860 and let's say we made it 58. 133 00:07:18,860 --> 00:07:20,800 In this case, I get back false, 134 00:07:20,800 --> 00:07:22,360 when what I really wanted to do 135 00:07:22,360 --> 00:07:24,610 was change the value of grade, 136 00:07:24,610 --> 00:07:27,390 so of course I need a single equal sign 137 00:07:27,390 --> 00:07:29,230 in order to be able to do that, 138 00:07:29,230 --> 00:07:34,230 and now grade has a new value associated with it. 139 00:07:34,330 --> 00:07:37,220 Another potential logic error, would be, 140 00:07:37,220 --> 00:07:40,923 let's say I want to compare "X" to 85, 141 00:07:42,810 --> 00:07:45,360 and I have not defined "X" yet, 142 00:07:45,360 --> 00:07:48,910 in that case as we demonstrated once before, 143 00:07:48,910 --> 00:07:52,200 you get a name error because the name "X" 144 00:07:52,200 --> 00:07:56,673 does not yet exist in this interactive session.