1 00:00:00,420 --> 00:00:04,560 Hello, and welcome to Chapter Two. Hope you enjoyed Chapter One. 2 00:00:04,560 --> 00:00:08,950 It was one of the longer lectures. Trying to motivate you a little bit. 3 00:00:08,950 --> 00:00:12,050 And now we're going to kind of go back to the basics, to the, chapter 4 00:00:12,050 --> 00:00:15,950 Chapter One covered sort of the first four to five chapters of the book. 5 00:00:15,950 --> 00:00:20,140 So as always, this this video, these slides are 6 00:00:20,140 --> 00:00:23,620 copyright Creative Common Attribution, as well as the audio. 7 00:00:24,810 --> 00:00:25,380 And so, 8 00:00:26,390 --> 00:00:28,430 now we're going to talk about sort of the really 9 00:00:28,430 --> 00:00:32,876 low-level things that make up the Python language. 10 00:00:32,876 --> 00:00:36,533 Constants. So I'm going to summarize this terminology just so I 11 00:00:36,533 --> 00:00:39,950 can like say the word "constant" and you won't freak out. 12 00:00:39,950 --> 00:00:45,590 A constant is as contrasted with something that changes, a variable. 13 00:00:45,590 --> 00:00:48,840 We talk about variables in the next slide. But for now, constants. 14 00:00:48,840 --> 00:00:51,480 Constants are in things that are sort 15 00:00:51,480 --> 00:00:54,150 of natural and instinctive. Things like numbers. 16 00:00:54,150 --> 00:00:56,656 A hundred and twenty-three. 17 00:00:56,656 --> 00:00:59,170 98.6, or Hello world. 18 00:00:59,170 --> 00:01:02,600 And so in, in, what, what I'm doing here is we're, we're using 19 00:01:02,600 --> 00:01:06,150 a Python interpreter, and that, that's how you can tell, the chevron prompt. 20 00:01:06,150 --> 00:01:10,296 And I'm saying print 123, and then Python responds with 123, 21 00:01:10,296 --> 00:01:16,420 print 98.6, Python responds with 98.6, and print 'Hello world'. 22 00:01:16,420 --> 00:01:21,305 So the constants are the 123, 98.6, and 'Hello world'. 23 00:01:21,305 --> 00:01:22,770 So these are things. 24 00:01:22,770 --> 00:01:26,930 We can use either single quotes or double quotes to make strings. 25 00:01:26,930 --> 00:01:29,730 And so programs kind of work with numbers and work with 26 00:01:29,730 --> 00:01:34,720 strings and we have these non-varying values that we call constants. 27 00:01:34,720 --> 00:01:39,940 So the other side of the picture is a variable. 28 00:01:39,940 --> 00:01:42,070 And the way I like to characterize a variable 29 00:01:42,070 --> 00:01:44,740 is it's a place in the memory of the computer. 30 00:01:46,100 --> 00:01:47,590 We give it a name as a programmer. 31 00:01:47,590 --> 00:01:49,530 We pick the variable name. 32 00:01:49,530 --> 00:01:55,130 In this, I'm saying x equals 12.2 and y equals 14. 33 00:01:55,130 --> 00:01:58,490 I am choosing the name and I'm choosing what to put in there. 34 00:01:59,610 --> 00:02:04,040 This is a statement called an assignment statement, and the way to 35 00:02:04,040 --> 00:02:07,290 think of the assignment statement is that it sort of has a direction. 36 00:02:08,370 --> 00:02:12,010 We're saying, dear Python, go find some memory. 37 00:02:12,010 --> 00:02:15,780 I will use label x later to, to refer to that 38 00:02:15,780 --> 00:02:19,680 memory, and take the number 12.2 and stick it into x. 39 00:02:19,680 --> 00:02:21,460 Then this is sequential code. 40 00:02:21,460 --> 00:02:24,360 Then the next thing I want you to do is I'd like you to go find some 41 00:02:24,360 --> 00:02:30,950 more memory, call it y, I will call it y later, and stick 14 in there, okay? 42 00:02:30,950 --> 00:02:34,280 And so that ends up sort of with two little areas 43 00:02:34,280 --> 00:02:34,800 of memory. 44 00:02:36,500 --> 00:02:38,950 You know, the one labeled x, and here's a 45 00:02:38,950 --> 00:02:41,930 little cell in which we, like a drawer, or something. 46 00:02:41,930 --> 00:02:44,960 And one labeled y. And we put 12.2. After these 47 00:02:44,960 --> 00:02:49,550 lines run, we have 12.2 in one and 14 in the other. 48 00:02:49,550 --> 00:02:55,050 Then, for example, if there's another line that's down here, so there's this 49 00:02:55,050 --> 00:02:59,250 third line after this has happened, after this has happened, x equals 100. 50 00:02:59,250 --> 00:03:02,300 Remember, this has kind of got an, a direction to it, see? 51 00:03:02,300 --> 00:03:07,460 Oh, remember that x that I had, you know, I would like now to put 100 in that. 52 00:03:07,460 --> 00:03:09,770 So as I'm thinking this through, I think of that as sort of 53 00:03:09,770 --> 00:03:14,920 removing the 12.2 or overwriting the 12.2 and putting 100 in its place. 54 00:03:14,920 --> 00:03:20,830 And so at the end here, x is left with 100 and y is left with 1 4 with 14. 55 00:03:20,830 --> 00:03:24,510 So these variables can kind of have one value in them and 56 00:03:24,510 --> 00:03:26,100 what we can look at them and we can 57 00:03:26,100 --> 00:03:28,850 reuse them and put different values in if we want. 58 00:03:30,900 --> 00:03:33,870 There are some rules for naming your variables. 59 00:03:33,870 --> 00:03:35,840 Again, you get to pick the variable names. 60 00:03:37,290 --> 00:03:39,530 Often we pick variables that make some sense. 61 00:03:39,530 --> 00:03:41,560 We'll talk about that in a second. 62 00:03:41,560 --> 00:03:44,900 In Python variables can start with an underscore. 63 00:03:44,900 --> 00:03:48,090 We tend not to, as normal programmers, use those. 64 00:03:48,090 --> 00:03:52,330 We let libraries use those. 65 00:03:52,330 --> 00:03:54,880 It has to have letters, numbers, and underscores. 66 00:03:54,880 --> 00:03:56,230 And, and start 67 00:03:56,230 --> 00:04:00,030 with start with a letter or an underscore. 68 00:04:00,030 --> 00:04:05,600 Case matters, so spam is good, eggs is 69 00:04:05,600 --> 00:04:08,300 good, spam23 is good because the number is not 70 00:04:08,300 --> 00:04:11,860 the first character, _speed, that's also perfectly fine 71 00:04:11,860 --> 00:04:13,605 because it start with an underscore or a letter. 72 00:04:13,605 --> 00:04:20,840 [COUGH] 23Spam starts with a letter, starts with a number, so that's bad. 73 00:04:20,840 --> 00:04:21,290 This starts with something 74 00:04:21,290 --> 00:04:24,390 other than a letter or an underscore. 75 00:04:24,390 --> 00:04:28,050 And you can't use a dot in the variable name. 76 00:04:28,050 --> 00:04:31,280 It turns out the dot has meaning to Python that would confuse it. 77 00:04:34,130 --> 00:04:37,080 That would confuse it and wouldn't understand [COUGH] what we 78 00:04:37,080 --> 00:04:39,150 really mean there, and so that would be a syntax error. 79 00:04:39,150 --> 00:04:41,030 That would be a syntax error. 80 00:04:41,030 --> 00:04:45,850 Because case is sensitive, that means that things like all lowercase 81 00:04:45,850 --> 00:04:49,720 spam is different than a upper case S and all uppercase. 82 00:04:49,720 --> 00:04:54,990 These are three distinct variables that are unique. 83 00:04:54,990 --> 00:04:58,590 Most people don't use, choose variables that might be so confusing. 84 00:04:58,590 --> 00:04:59,440 So that's to 85 00:04:59,440 --> 00:05:01,860 you as you write it and as to anybody that 86 00:05:01,860 --> 00:05:06,400 might read it would find three variables named this very confusing. 87 00:05:06,400 --> 00:05:08,060 So it's a bad idea. 88 00:05:08,060 --> 00:05:10,160 Don't do it, but I'm just showing you as 89 00:05:10,160 --> 00:05:14,900 an example that case can make a variable name distinct. 90 00:05:14,900 --> 00:05:18,170 And again, this variable is a place in memory 91 00:05:18,170 --> 00:05:22,400 that we are going to store and retrieve information. 92 00:05:22,400 --> 00:05:24,680 Whether that be numbers or strings or whatever. 93 00:05:24,680 --> 00:05:25,840 These are things that we control. 94 00:05:27,010 --> 00:05:30,160 Now Python also has a set of reserved words. 95 00:05:30,160 --> 00:05:33,040 What it really means is you can't use these for variables. 96 00:05:33,040 --> 00:05:38,854 These words have very special meaning. And, for, is, raise, if. 97 00:05:38,854 --> 00:05:44,610 So you can't make a variable named i-f. It would be like, oh no, that is "if". 98 00:05:44,610 --> 00:05:45,470 I know what "if" is. 99 00:05:45,470 --> 00:05:49,350 So these are words that Python has as its core vocabulary. 100 00:05:49,350 --> 00:05:51,180 And forbids you to use them 101 00:05:51,180 --> 00:05:55,610 for other purposes, like variable names or later function names. 102 00:05:55,610 --> 00:06:03,000 So that's kind of the vocabulary. Constants, variables, and reserved words. 103 00:06:03,000 --> 00:06:05,990 Now, we take these and we start assembling them 104 00:06:05,990 --> 00:06:10,580 into sort of sentences, statements, Python statements that do something. 105 00:06:10,580 --> 00:06:13,290 So we've already talked about an assignment statement. 106 00:06:13,290 --> 00:06:15,200 It has kind of an arrow here. 107 00:06:15,200 --> 00:06:18,210 It says, hey Python, go find me a place called x. 108 00:06:18,210 --> 00:06:22,710 Take the number 2 and stick it in there for later, then continue on. 109 00:06:22,710 --> 00:06:26,990 Now, because there's an arrow, the right side of this is done first. 110 00:06:26,990 --> 00:06:31,360 And so it said, so this right side, you can kind of ignore for the moment the 111 00:06:31,360 --> 00:06:33,410 left-hand side and it calculates the right-hand 112 00:06:33,410 --> 00:06:35,530 side by looking at the current value for x. 113 00:06:35,530 --> 00:06:40,620 Which happens to be 2, and adds these two things together, and then gets 4. 114 00:06:40,620 --> 00:06:44,910 And then, at the point where it knows 4, that this 115 00:06:44,910 --> 00:06:48,740 number is 4, it will then store that back into X. 116 00:06:48,740 --> 00:06:53,790 And so then, later, we print x and we will get the 4. And so again, this is 117 00:06:53,790 --> 00:06:57,180 a sequence of steps and the, the variable x 118 00:06:57,180 --> 00:07:00,800 changes as these steps continue. And when we're saying print x, 119 00:07:00,800 --> 00:07:03,720 that really means print the current value for x. 120 00:07:07,630 --> 00:07:12,580 So, we can do a number of different operators and assignment statements. 121 00:07:12,580 --> 00:07:15,960 We calculate this right-hand side. 122 00:07:15,960 --> 00:07:19,170 This is sort of all calculated, whatever this is, based on 123 00:07:19,170 --> 00:07:22,450 the current value for x does this calculation, and then when 124 00:07:22,450 --> 00:07:25,570 it knows what the answer is, it assigns that into the 125 00:07:25,570 --> 00:07:28,586 variable that's on the left-hand side of the assignment statement. 126 00:07:28,586 --> 00:07:32,730 Again, calculate the right-hand 127 00:07:32,730 --> 00:07:35,670 side completely and then move it to the left-hand side. 128 00:07:35,670 --> 00:07:38,790 Some early languages actually didn't use 129 00:07:38,790 --> 00:07:41,280 the equals sign for the assignment operator. 130 00:07:41,280 --> 00:07:46,390 This assignment operator in, in a way it kind of [INAUDIBLE] 131 00:07:46,390 --> 00:07:46,790 Some languages 132 00:07:46,790 --> 00:07:50,750 An early language actually used an arrow. 133 00:07:50,750 --> 00:07:53,110 Arrows aren't really on people's keyboards. 134 00:07:53,110 --> 00:07:57,950 Another language used colon equals as this assignment operator. 135 00:07:57,950 --> 00:07:58,890 But we use equals. 136 00:07:58,890 --> 00:08:03,800 Now, if you're familiar with math this can be a little confusing, like x equals 1 137 00:08:03,800 --> 00:08:07,780 and then X equals 2. That as mathematics would be bad math 138 00:08:07,780 --> 00:08:11,580 because in a proof or a problem, x can only have one value. 139 00:08:11,580 --> 00:08:14,740 But in programming if this was two statements, that means 140 00:08:14,740 --> 00:08:17,935 just x had a value, and then the value for x changed later. 141 00:08:17,935 --> 00:08:23,150 Okay. So just kind of go through this because it's 142 00:08:23,150 --> 00:08:27,090 working from the right-hand side to the left-hand side on assignment statements. 143 00:08:27,090 --> 00:08:30,940 It is pulling out these x values, so x may have 0.6. 144 00:08:30,940 --> 00:08:35,680 It pulls the values out before, sort of ignoring this part 145 00:08:35,680 --> 00:08:39,280 right here, and it's just going to try to resolve this expression. 146 00:08:39,280 --> 00:08:42,570 And it has multiplication and parentheses and things like that. 147 00:08:42,570 --> 00:08:46,480 So it basically pulls the 0.6 into the calculation, 148 00:08:46,480 --> 00:08:48,610 does the 1 minus x, which gives you 0.4. 149 00:08:48,610 --> 00:08:52,660 Then it multiplies these three things together, giving 0.93. 150 00:08:52,660 --> 00:08:56,904 And then when it is all done with all of that, it takes that. 151 00:08:56,904 --> 00:08:57,270 Oops. 152 00:08:57,270 --> 00:09:03,980 It takes that 0.93, and then puts it back into x. 153 00:09:03,980 --> 00:09:08,230 And so this is just sort of emphasizing how the right-hand side is computed to 154 00:09:08,230 --> 00:09:13,630 produce a value, then it is moved into the variable, and that is why you 155 00:09:13,630 --> 00:09:15,995 can have sort of x on both sides. 156 00:09:15,995 --> 00:09:20,140 Because this is like the old, and this is the new. 157 00:09:20,140 --> 00:09:23,690 This is the old x participates in the calculation, and 158 00:09:23,690 --> 00:09:27,720 then when the calculation is done, it becomes the new x. 159 00:09:27,720 --> 00:09:28,410 Hope that makes sense. 160 00:09:29,500 --> 00:09:32,980 So, this, on the right-hand side here is a numeric expression. 161 00:09:32,980 --> 00:09:35,650 So we have a number of different operators. 162 00:09:35,650 --> 00:09:38,760 Some of them are instinctive, intuitive. 163 00:09:38,760 --> 00:09:40,260 The plus and the minus. 164 00:09:40,260 --> 00:09:43,310 The reason some of these are so weird is in the really old days, we 165 00:09:43,310 --> 00:09:45,950 didn't have too many things on the keyboard, 166 00:09:45,950 --> 00:09:48,100 and a lot of programs were very mathematical. 167 00:09:48,100 --> 00:09:49,410 And so they figured out what was on 168 00:09:49,410 --> 00:09:52,220 the keyboard of the computer equipment of the day. 169 00:09:52,220 --> 00:09:55,110 And then they had to fake certain things. 170 00:09:55,110 --> 00:09:57,750 So, it turns out that plus and minus were on the keyboard, 171 00:09:57,750 --> 00:10:02,620 and so plus and minus are addition and subtraction, respectively. 172 00:10:02,620 --> 00:10:04,140 There was no kind of times 173 00:10:04,140 --> 00:10:08,360 operator for multiplication, and dot was used for decimal points. 174 00:10:08,360 --> 00:10:12,930 So they used asterisk for multiplication. So on computers' languages, nearly 175 00:10:12,930 --> 00:10:17,760 all of them, they basically use a mult times for multiplication. 176 00:10:17,760 --> 00:10:19,790 Slash is used for division. 177 00:10:19,790 --> 00:10:23,460 So we say like, 8/2, which is 8 divided by 2. 178 00:10:24,642 --> 00:10:29,727 Raising something to the power like 4 squared, 179 00:10:29,727 --> 00:10:35,680 that is double asterisk. And then remainder is if you 180 00:10:35,680 --> 00:10:40,480 do a division that gives you the remainder rather than divisor. 181 00:10:40,480 --> 00:10:44,580 So 8 over 2 is 4 remainder 0. So 182 00:10:44,580 --> 00:10:48,100 the remainder is what you get with this particular operator. 183 00:10:48,100 --> 00:10:49,900 There's a few cool things that we can do 184 00:10:49,900 --> 00:10:52,610 with remainder that we won't talk about right away. 185 00:10:52,610 --> 00:10:54,660 But it's there. 186 00:10:54,660 --> 00:10:57,470 And so here's just a couple of sample expressions. 187 00:10:58,888 --> 00:11:03,270 That's giving me green. 188 00:11:03,270 --> 00:11:04,240 Okay. 189 00:11:04,240 --> 00:11:06,690 So, so again, I'm using a Python Interpreter. 190 00:11:06,690 --> 00:11:08,310 So you can kind of, this is just the prompt. 191 00:11:08,310 --> 00:11:10,750 These chevrons are the prompt. 192 00:11:10,750 --> 00:11:13,760 Create the variable xx, and assign it to 2. 193 00:11:13,760 --> 00:11:16,580 Retrieve the old value and an addition. 194 00:11:16,580 --> 00:11:20,050 Then print it out and put it back into xx so xx 195 00:11:20,050 --> 00:11:24,110 has 4. yy, this is a multiplication, 440 times 12. 196 00:11:24,110 --> 00:11:28,780 It is 5,280. yy over 1,000. Now this is a little counter-intuitive 197 00:11:28,780 --> 00:11:35,060 Because yy is an integer, it then does it in a truncated division. 198 00:11:35,060 --> 00:11:42,080 And so, 5,280 divided by 1000 is 5. Now if, and, 199 00:11:42,080 --> 00:11:45,160 and so that's an integer division. We'll see in a second 200 00:11:45,160 --> 00:11:46,490 about floating point division. 201 00:11:47,990 --> 00:11:51,570 Now we take the variable jj and we set it to 23. 202 00:11:51,570 --> 00:11:55,860 And now we're going to use the modular or modulo or remainder operator. 203 00:11:55,860 --> 00:12:01,610 Say what is jj, what is the remainder when divide this jj by 5. 204 00:12:01,610 --> 00:12:04,710 And so if you think about this, we take old long division, 205 00:12:04,710 --> 00:12:09,187 23 divided by 5, you end up with 4 and then remainder 3. 206 00:12:10,590 --> 00:12:13,010 The modulo operator, or the percent of 207 00:12:13,010 --> 00:12:16,090 the remainder operator, gives us back this number. 208 00:12:16,090 --> 00:12:18,670 And so that's why kk is 3. 209 00:12:18,670 --> 00:12:22,780 It is the remainder of 23 when divided by 5, 210 00:12:22,780 --> 00:12:27,670 or the remainder of the division of 5 into 23. 211 00:12:27,670 --> 00:12:32,090 And the raising to the power, 4 cubed. That's not so nice. 212 00:12:32,090 --> 00:12:34,550 4 cubed is 4 star, star 3. 213 00:12:34,550 --> 00:12:35,842 And so that ends up being 64. 214 00:12:35,842 --> 00:12:42,400 So that's just operations. Now, just like in algebra and mathematics 215 00:12:43,510 --> 00:12:49,650 we have rules about when to which, which operations happen first. 216 00:12:49,650 --> 00:12:52,940 In general, things like the power happens before the 217 00:12:52,940 --> 00:12:56,030 multiplication and division, and then the addition and subtraction happen. 218 00:12:56,030 --> 00:12:58,630 And so there are some rules that, when 219 00:12:58,630 --> 00:13:00,940 you're looking at an expression and trying to calculate 220 00:13:00,940 --> 00:13:05,910 what its value is, if you don't have parentheses, it follows these rules. 221 00:13:05,910 --> 00:13:10,120 And so the, the most, the rule that sort of 222 00:13:10,120 --> 00:13:14,430 trumps all the rules is that parentheses are always respected. 223 00:13:14,430 --> 00:13:16,328 So a lot of us just write these with 224 00:13:16,328 --> 00:13:19,550 parentheses in place, even sometimes though you don't need it. 225 00:13:21,100 --> 00:13:24,930 Then after parentheses have been handled, then it does exponentiation. 226 00:13:24,930 --> 00:13:26,220 Then it does multiplication, 227 00:13:26,220 --> 00:13:30,188 division, and remainder. And then it does addition and subtraction. 228 00:13:30,188 --> 00:13:33,960 And then, when all else being equal, it just works left to right. 229 00:13:33,960 --> 00:13:40,370 So let's, let's look through an example. So here is a 230 00:13:40,370 --> 00:13:47,250 calculation that is, you know, 1, 1 plus 2 times 3 divided 4 over 5. 231 00:13:47,250 --> 00:13:51,380 And the question is, what order does this happen, okay? 232 00:13:51,380 --> 00:13:53,470 And so let's sort of take a look at this. 233 00:13:55,210 --> 00:13:58,780 And so, we start with are there any parentheses? 234 00:13:58,780 --> 00:14:00,620 And the answer is no, there are no parentheses. 235 00:14:00,620 --> 00:14:05,100 So let's go next. Power. 236 00:14:05,110 --> 00:14:11,100 And so the, the power says okay, let's look across and find those things that 237 00:14:11,100 --> 00:14:17,380 are raised to a power. And 2 cubed or 2 to the third power is the, the power. 238 00:14:17,380 --> 00:14:20,080 So we're going to do that one. Okay. 239 00:14:20,080 --> 00:14:21,880 And then we can, the way I do it when 240 00:14:21,880 --> 00:14:24,150 I'm sort of doing these slowly is I rewrite it. 241 00:14:24,150 --> 00:14:28,710 So the 2 to the third power becomes 8, so it's 1 plus 8 over 4 times 5. 242 00:14:28,710 --> 00:14:31,730 And then now we can say oh power, that's taken care of. 243 00:14:31,730 --> 00:14:35,540 Now we're going to do multiplication and division and we go across. 244 00:14:35,540 --> 00:14:38,620 Now we have both a division and multiplication. 245 00:14:38,620 --> 00:14:40,310 Okay? Multiplication and division are done at the same 246 00:14:40,310 --> 00:14:42,630 time, so that means we do left to right, 247 00:14:42,630 --> 00:14:45,105 which means we do the first one we encounter first. 248 00:14:45,105 --> 00:14:53,500 And so that will be 8 over 4 because of the left-to-right rule. 249 00:14:53,500 --> 00:14:55,220 And so we find that one, and that's the 250 00:14:55,220 --> 00:14:58,520 one that gets computed next, and that turns into 2. 251 00:14:58,520 --> 00:15:00,550 And again, I like to rewrite these expressions 252 00:15:00,550 --> 00:15:03,910 just to keep my brain really, really clear. 253 00:15:03,910 --> 00:15:06,860 After a while you just do it in your head, but I rewrite them. 254 00:15:06,860 --> 00:15:07,700 When I was first learning it, 255 00:15:07,700 --> 00:15:09,070 at least, I rewrote it all the time. 256 00:15:10,620 --> 00:15:15,480 And and so next looking at this, there's a multiplication. 257 00:15:15,480 --> 00:15:19,660 We're not done with multiplication yet. So the 2 over 5 is the next thing. 258 00:15:21,040 --> 00:15:24,890 And then we do that calculation, and that becomes 10, and again we rewrite it. 259 00:15:24,890 --> 00:15:28,670 And now we've done the multiplication, and we're going to do addition next. 260 00:15:28,670 --> 00:15:33,950 And that's just 1 over 10, and that becomes 11. 261 00:15:33,950 --> 00:15:36,540 And so basically, this big long thing, 262 00:15:36,540 --> 00:15:40,700 through a series of successive steps, becomes 11. 263 00:15:40,700 --> 00:15:44,110 And indeed, when we print it out, that's what we get. 264 00:15:44,110 --> 00:15:44,610 Okay? 265 00:15:46,810 --> 00:15:49,140 So, there's the rules that are parentheses, 266 00:15:49,140 --> 00:15:52,440 power, multiplication, addition, and then, left to right. 267 00:15:52,440 --> 00:15:58,710 But smart people usually just put parentheses in, you know? 268 00:15:58,710 --> 00:16:01,760 So here's this, here's an exam. Oop, go back, go back. 269 00:16:01,760 --> 00:16:03,360 Here's an exam question. 270 00:16:03,360 --> 00:16:09,040 Now, I wouldn't write this code, right, I wouldn't write this code this way. 271 00:16:09,040 --> 00:16:11,270 I would put a parentheses here. 272 00:16:12,360 --> 00:16:13,100 And a parentheses there. 273 00:16:14,600 --> 00:16:18,920 It's the same thing because that's exactly the 2 times 3 is going to happen and 274 00:16:18,920 --> 00:16:20,800 4 over 5 is going to happen and then the 275 00:16:20,800 --> 00:16:23,140 plus and the minus will happen left to right. 276 00:16:23,140 --> 00:16:25,930 But why not make it easier on your readers 277 00:16:25,930 --> 00:16:28,430 and just put the parentheses in. Because they're redundant. 278 00:16:28,430 --> 00:16:31,460 They're not necessary, but away you go. 279 00:16:31,460 --> 00:16:34,656 Now, if you don't want it to happen in that order, of 280 00:16:34,656 --> 00:16:38,110 course then you have to put parentheses if you want the addition 281 00:16:38,110 --> 00:16:40,890 to happen before the multiplication, then you 282 00:16:40,890 --> 00:16:43,190 have to put parentheses in, which you can. 283 00:16:43,190 --> 00:16:48,500 But we tend to recommend that you use more parentheses rather than less parentheses. 284 00:16:49,690 --> 00:16:53,530 Now, Python integer division in Python 2, 285 00:16:53,530 --> 00:16:56,120 which we're using Python 2 for this class. 286 00:16:56,120 --> 00:17:00,090 There's a new Python 3 that the world is slowly transitioning 287 00:17:00,090 --> 00:17:03,060 to and a lot of people are using it in teaching. 288 00:17:03,060 --> 00:17:08,370 But it's not as common, sort of, in the real world with libraries and utilities. 289 00:17:08,370 --> 00:17:10,671 And so we'll stick with Python 2 for a few 290 00:17:10,671 --> 00:17:14,890 more years until Python 3 really kind of turns the corner. 291 00:17:14,890 --> 00:17:17,910 It's nice to have it there, but there's so much Python and it's so 292 00:17:17,910 --> 00:17:22,960 popular, Python 2, that it's just kind of hard to get everybody up to Python 3. 293 00:17:22,960 --> 00:17:28,900 So in Python 2, integer division truncates and you saw that before where 294 00:17:28,900 --> 00:17:34,060 I did the 5280 by 1000 and I got 5 as and, and, but we 295 00:17:34,060 --> 00:17:38,030 can look at a couple of examples that make this really very quite, quite clear. 296 00:17:38,030 --> 00:17:40,820 So, 10 divided by 2 is 5 as you would expect. 297 00:17:40,820 --> 00:17:42,960 9 Divided by 2 is 4. 298 00:17:42,960 --> 00:17:44,630 Not exactly what you'd expect. 299 00:17:44,630 --> 00:17:49,220 You kind of expect that to be 4.5, instead of 4. 300 00:17:49,220 --> 00:17:53,950 But in Python 3, it will be 4.5, but for now, in Python 2, 301 00:17:53,950 --> 00:18:00,920 9 over, 9 over 2 is 4. And 99 over 100 is 0. 302 00:18:00,920 --> 00:18:03,520 Now that seems rather counter-intuitive, but it is a truncating 303 00:18:03,520 --> 00:18:07,150 division, it's not a rounding division, it's a truncating division. 304 00:18:07,150 --> 00:18:10,980 Now, interestingly, if you make either of these numbers have a decimal, make them 305 00:18:10,980 --> 00:18:16,282 what we call floating point numbers, then the division is done in floating point. 306 00:18:16,282 --> 00:18:19,530 So, 10.0 over 2.0 307 00:18:19,530 --> 00:18:24,090 is 5.0. Now, these are different. 308 00:18:24,090 --> 00:18:26,873 This is an integer number, and this is a floating point number. 309 00:18:26,873 --> 00:18:27,836 It's 5.0. 310 00:18:27,836 --> 00:18:31,830 And then 99.0 over 100.0 is exactly as you 311 00:18:31,830 --> 00:18:34,580 would expect, and it's a floating point number, so. 312 00:18:36,830 --> 00:18:41,310 Now you can also mix integers and floating point numbers as you go. 313 00:18:41,310 --> 00:18:43,430 So here we have 99 over 100. 314 00:18:43,430 --> 00:18:46,890 Those are both integers. Integer, integer. 315 00:18:46,890 --> 00:18:50,010 And, or, and that comes out with 0 because it's truncating. 316 00:18:50,010 --> 00:18:53,080 Now if we have an integer and a floating point 317 00:18:53,080 --> 00:18:57,191 number, 99 over 100.0, then that comes out as 0.99. 318 00:18:58,350 --> 00:19:01,990 And either one, if we have 99 over 100, that's a floating point, and 319 00:19:01,990 --> 00:19:02,710 that's an integer. 320 00:19:02,710 --> 00:19:06,520 We still end up with a floating point, so this is a floating point, floating point. 321 00:19:06,520 --> 00:19:11,320 And even in complex expressions, as it evaluates when 322 00:19:11,320 --> 00:19:13,270 it sees an integer, so the first thing when 323 00:19:13,270 --> 00:19:20,820 you evaluate is this would become a 6, so it would be 1 plus 6 over 4.0 minus 5. 324 00:19:20,820 --> 00:19:27,461 Then it would be doing the 6 over 4.0 and that would be 1.5, 1 plus 1.5 325 00:19:27,461 --> 00:19:30,900 minus 5. And so this is an integer and that's 326 00:19:30,900 --> 00:19:33,740 a floating point and the result becomes a floating point. 327 00:19:33,740 --> 00:19:36,530 And then the rest of the calculation is done floating point 328 00:19:36,530 --> 00:19:41,200 to the point where the ultimate is a floating point negative 2.5. 329 00:19:41,200 --> 00:19:45,260 So you can throw a floating point into a calculation and as soon as the 330 00:19:45,260 --> 00:19:48,290 calculation touches the floating point, the remainder 331 00:19:48,290 --> 00:19:50,840 of the calculation is done in floating point. 332 00:19:50,840 --> 00:19:52,644 It kind of converts at the floating point but it doesn't 333 00:19:52,644 --> 00:19:55,830 want to convert it back because it considers floating 334 00:19:55,830 --> 00:19:59,429 point sort of the more general of the representations. 335 00:20:01,800 --> 00:20:06,540 So, here we are, talking about integers and floating points. 336 00:20:06,540 --> 00:20:10,520 These are a concept in programming languages and in Python called type. 337 00:20:11,820 --> 00:20:14,270 Variables and constants have a type. 338 00:20:15,600 --> 00:20:18,540 We can see that if you say 1, versus 1.0, 339 00:20:18,540 --> 00:20:22,090 they have different, they, it works different, it functions differently. 340 00:20:22,090 --> 00:20:28,300 And so Python keeps track of both variables and literals/constants, and 341 00:20:28,300 --> 00:20:32,240 having them have a type. And we've seen this, right? 342 00:20:32,240 --> 00:20:34,920 Now, the interesting thing is, is Python is very aware of 343 00:20:34,920 --> 00:20:39,920 the type and can use the same syntax to accomplish different things. 344 00:20:39,920 --> 00:20:44,200 So if we look at this line here, where we say dd equals 1 plus 4. 345 00:20:44,200 --> 00:20:45,790 Well it looks at the 1 and looks at the 4 and it says, 346 00:20:45,790 --> 00:20:48,770 oh those are two integers. I will add those together and give you a 5. 347 00:20:48,770 --> 00:20:53,210 So it gives you an integer, an integer, and an integer comes back, 348 00:20:53,210 --> 00:20:53,310 Okay? 349 00:20:53,310 --> 00:20:57,820 And then ee equals 'hello ' plus 'there'. Well these are two strings, 350 00:20:57,820 --> 00:21:02,798 'hello ' and 'there'. And it says hmm, this must be a concatenation. 351 00:21:02,798 --> 00:21:07,040 Alright? So I'm going to concatenate those together because 352 00:21:07,040 --> 00:21:10,070 those are strings and I know how to concatenate strings. 353 00:21:10,070 --> 00:21:12,660 And that's kind of like string addition, right? 354 00:21:14,060 --> 00:21:18,460 And so we see a "hello there" as a result. Now the interesting thing is, where 355 00:21:18,460 --> 00:21:21,760 did this space come from? Let me change colors here. 356 00:21:21,760 --> 00:21:22,690 Oops. 357 00:21:22,690 --> 00:21:27,050 Where did that space come from? Well, the plus does not add the space. 358 00:21:27,050 --> 00:21:29,750 Here's a space right there, and that's the space. 359 00:21:29,750 --> 00:21:34,930 So I can concatenate it, hello space plus there, and that's how I got hello there. 360 00:21:34,930 --> 00:21:36,690 But, the key thing is, is this plus 361 00:21:36,690 --> 00:21:42,930 operator, clear, this plus operator looks to either side 362 00:21:42,930 --> 00:21:43,700 and says oh, 363 00:21:43,700 --> 00:21:46,790 they're strings. I think you mean concatenation. 364 00:21:46,790 --> 00:21:49,290 Here it looks either side and says oh, 365 00:21:49,290 --> 00:21:51,970 those are integers, I think you mean addition. 366 00:21:51,970 --> 00:21:57,610 So Python is very aware of type and type informs Python what you really mean. 367 00:21:57,610 --> 00:21:58,952 So, it looks like those are kind 368 00:21:58,952 --> 00:22:01,270 of the same, but they're quite different operations. 369 00:22:03,990 --> 00:22:09,090 So the type can get you into trouble. Remember Python is looking at the type. 370 00:22:09,090 --> 00:22:10,840 So here we have a little problem, our 371 00:22:10,840 --> 00:22:14,880 first traceback, first of many tracebacks. 372 00:22:14,880 --> 00:22:19,500 So here we have ee which is hello there which is 373 00:22:19,500 --> 00:22:22,330 exactly what we did. This is a string and this is a string. 374 00:22:22,330 --> 00:22:27,150 So ee should be a string. And then we try to add 1 to it. 375 00:22:27,150 --> 00:22:29,170 And again, Python is saying oh, I see 376 00:22:29,170 --> 00:22:32,130 a plus sign here, so I'm going to look over here, yeah, 377 00:22:32,130 --> 00:22:34,240 that's a string, and look over here, and that's an integer. 378 00:22:34,240 --> 00:22:37,780 And it's like, aaah! And this is a traceback. 379 00:22:37,780 --> 00:22:41,070 Now, here's a good time to talk about tracebacks. 380 00:22:41,070 --> 00:22:43,370 Tracebacks, I color them red. 381 00:22:43,370 --> 00:22:47,480 Because you might think that Python dislikes you or 382 00:22:47,480 --> 00:22:50,780 thinks that you're, you know, unworthy of its brilliance. 383 00:22:51,860 --> 00:22:54,280 And certainly the way these things are worded it sounds like, 384 00:22:54,280 --> 00:22:58,190 you know, the, you're being scolded. It's like, hey, type error. 385 00:22:58,190 --> 00:23:01,800 You can, cannot concatenate str and int objects, right? 386 00:23:01,800 --> 00:23:05,800 That's, I'm, I'm scolding you, you bad, bad programmer. 387 00:23:05,800 --> 00:23:08,040 And it does feel a bit like you're scolded. 388 00:23:08,040 --> 00:23:11,740 But, if you go back to lecture one, this is also 389 00:23:11,740 --> 00:23:16,010 the moment where, really, we shouldn't think of this as like scolding. 390 00:23:16,010 --> 00:23:18,550 We should think of this as Python sort of asking for help. 391 00:23:18,550 --> 00:23:19,700 It's like, 392 00:23:19,700 --> 00:23:25,510 wow, you gave me this line, and I, Python, have no idea. 393 00:23:25,510 --> 00:23:28,970 In all your greatness, could you give me some possible 394 00:23:28,970 --> 00:23:30,810 clue as to what you really mean for me to do? 395 00:23:30,810 --> 00:23:32,260 Because I'm so lost. 396 00:23:32,260 --> 00:23:35,730 And given that I'm Python and I'm lost and you are the only 397 00:23:35,730 --> 00:23:41,420 purpose for my existence, I must stop until you give me better guidance. 398 00:23:41,420 --> 00:23:44,554 So, don't look at tracebacks as scolding. 399 00:23:44,554 --> 00:23:50,305 They sound like scolding. I'll stop coloring them red after a while. 400 00:23:50,305 --> 00:23:54,360 So, if Python is so obsessed with the type of things, you 401 00:23:54,360 --> 00:23:57,030 should be able to ask Python what the type of something is. 402 00:23:57,030 --> 00:23:59,720 So there's a built-in function called type. 403 00:23:59,720 --> 00:24:01,410 This is part of the Python language. 404 00:24:01,410 --> 00:24:04,750 Type (), and you can put a variable in here. 405 00:24:04,750 --> 00:24:06,450 What's the type of the variable ee? 406 00:24:06,450 --> 00:24:10,180 And it says, oh yeah, I know what that is, that would be a string. 407 00:24:10,180 --> 00:24:12,060 And then you can also put a constant in here. 408 00:24:12,060 --> 00:24:15,640 And say what's the type of quote, hello, quote, and that's a string too. 409 00:24:15,640 --> 00:24:17,210 And what's the type of the number 1? 410 00:24:17,210 --> 00:24:19,010 Well that would be an integer. 411 00:24:19,010 --> 00:24:20,660 So it's picky about the type, but it will 412 00:24:20,660 --> 00:24:23,630 also share with you what it believes the type is. 413 00:24:24,990 --> 00:24:28,120 And there's several types of numbers. 414 00:24:28,120 --> 00:24:31,540 As I've already mentioned, there are integers, which are the whole numbers. 415 00:24:31,540 --> 00:24:33,960 They can be positive and negative and zero. 416 00:24:33,960 --> 00:24:35,310 And then there are the decimal numbers, 417 00:24:35,310 --> 00:24:42,060 the floating point numbers, like 98.6 or negative 2.5 or 14.0. 418 00:24:42,060 --> 00:24:46,308 Python knows these as well because it does division different if it's presented with 419 00:24:46,308 --> 00:24:49,610 two integers, or an integer and a float, or a float and a float. 420 00:24:54,100 --> 00:24:57,620 And so here we have x is 1, and we'll say what is it? 421 00:24:57,620 --> 00:24:58,890 It's an integer. 422 00:24:58,890 --> 00:25:01,600 And we say it's 98.6, and we'll say, well, what's that? 423 00:25:01,600 --> 00:25:02,810 It's a float. 424 00:25:02,810 --> 00:25:05,130 And you can ask for both variables and constants. 425 00:25:05,130 --> 00:25:07,400 So what's the type of 1? It's an integer. 426 00:25:07,400 --> 00:25:09,700 And what's type of up 1.0? And it's a float. 427 00:25:11,550 --> 00:25:12,850 You can also convert types. 428 00:25:12,850 --> 00:25:16,370 It has a bunch of type conversion functions built into it. 429 00:25:16,370 --> 00:25:19,170 So, there's implicit conversion going on 430 00:25:19,170 --> 00:25:23,110 when you're sort of saying, you know, divide an integer by a floating point. 431 00:25:23,110 --> 00:25:25,690 It says okay I see, I look to the sides and 432 00:25:25,690 --> 00:25:28,890 I will make the, I will make the conversion for you. 433 00:25:28,890 --> 00:25:30,400 But you can also be explicit. 434 00:25:30,400 --> 00:25:32,760 So in this case we're going to say, take this 435 00:25:32,760 --> 00:25:35,848 99 and convert to a floating point version of itself. 436 00:25:35,848 --> 00:25:39,060 Which is 99.0. And then do the division. 437 00:25:39,060 --> 00:25:41,920 So Python looks out here and goes oh, after that, that's 438 00:25:41,920 --> 00:25:44,920 a float, and that's an integer if I look over here. 439 00:25:44,920 --> 00:25:47,540 And then that means that the result is a float. 440 00:25:47,540 --> 00:25:49,360 And the division is done as a float. 441 00:25:49,360 --> 00:25:55,330 So we are force converting the 99 integer into a 99.0 float. 442 00:25:57,180 --> 00:25:59,460 And we can even do this like and just stick it in the variable. 443 00:25:59,460 --> 00:26:02,790 So we can just put 42 in i and that is an integer. 444 00:26:02,790 --> 00:26:06,530 Then we can say, hey, convert float that i 445 00:26:06,530 --> 00:26:09,750 into a float and stick it into the variable f. 446 00:26:09,750 --> 00:26:13,600 And so we can print it. And now it's 42.0 instead of 42. 447 00:26:13,600 --> 00:26:16,426 Right? They're not the same. 448 00:26:16,426 --> 00:26:18,180 They're both kind of 42, but one is a 449 00:26:18,180 --> 00:26:20,900 floating point 42 and the other is an integer 42. 450 00:26:20,900 --> 00:26:24,110 And we can ask, and that is a float. 451 00:26:24,110 --> 00:26:26,320 And you can also do the same thing in the middle of 452 00:26:26,320 --> 00:26:30,510 a calculation, where you have 1 plus 2 times a float of 3. 453 00:26:30,510 --> 00:26:34,812 This float is done quickly. So the first thing that happens 454 00:26:34,812 --> 00:26:39,130 this is 1 plus 2 times 3.0 over 4 minus 5. 455 00:26:39,130 --> 00:26:39,630 So 456 00:26:41,800 --> 00:26:43,540 the first thing that happens is these floats 457 00:26:43,540 --> 00:26:45,740 are done because they are parentheses so they matter. 458 00:26:45,740 --> 00:26:49,640 So this is a built-in function called float that takes, as its 459 00:26:49,640 --> 00:26:55,390 argument, a non-floating point number and gives you back a floating point number. 460 00:26:55,390 --> 00:26:57,390 We'll talk more about functions in Chapter Four. 461 00:27:01,230 --> 00:27:05,640 You can also convert between strings and numbers, and if you 462 00:27:05,640 --> 00:27:10,220 recall, I, we did the example where we took a string. 463 00:27:10,220 --> 00:27:12,880 In this case, I'm being a little confusing, because 464 00:27:12,880 --> 00:27:15,680 I'm making a string with the characters 1, 2, 3. 465 00:27:15,680 --> 00:27:19,110 Now, this is not the same as 123. 466 00:27:19,110 --> 00:27:23,530 This is a three-character string with 1, 2, 3 in it. 467 00:27:23,530 --> 00:27:26,460 And I can ask what kind of thing is in there, and it says, 468 00:27:26,460 --> 00:27:28,810 oh, there's a string in there. I know about that. 469 00:27:28,810 --> 00:27:30,490 And then I can try to add 1 to it, and 470 00:27:30,490 --> 00:27:35,690 it seems intuitive that quote 123 plus 1 would be somehow 124. 471 00:27:35,690 --> 00:27:37,966 But it's not. 472 00:27:37,966 --> 00:27:40,474 Python takes a look at the plus and says, oh there's 473 00:27:40,474 --> 00:27:43,440 a string on that side, and an integer on that side. 474 00:27:43,440 --> 00:27:45,720 I am going to freak out and tell you 475 00:27:45,720 --> 00:27:48,940 that you cannot concatenate a string and an integer. 476 00:27:48,940 --> 00:27:51,710 Okay? But there is an int function 477 00:27:51,710 --> 00:27:55,360 that converts various things, including strings, to an integer. 478 00:27:55,360 --> 00:28:00,520 So we can give as its parameter, its input, the string value, then it 479 00:28:00,520 --> 00:28:05,450 converts it to an integer, and then we'll put the result in the variable ival. 480 00:28:05,450 --> 00:28:09,840 We can ask what the type of that is, it's an i, it's a integer. 481 00:28:09,840 --> 00:28:13,360 And now we can use it in an expression, print ival plus 1, and 482 00:28:13,360 --> 00:28:17,230 so now Python looks to both sides, sees an integer, sees an integer, and 483 00:28:17,230 --> 00:28:19,530 gets 124. Voila. 484 00:28:21,090 --> 00:28:24,880 Now, if I make a new variable and I stick hello Bob in it, and I 485 00:28:24,880 --> 00:28:31,340 say hey, let's convert hello Bob to an integer, as you might expect, it blows up. 486 00:28:31,340 --> 00:28:33,850 And it says, invalid literal for int. 487 00:28:35,980 --> 00:28:42,310 These, these tracebacks again, once you kind of get used to the kind of harsh 488 00:28:42,310 --> 00:28:45,242 wording of them, because they're not saying, sorry, comma, 489 00:28:45,242 --> 00:28:48,010 they're trying to tell you what's going on. 490 00:28:48,010 --> 00:28:52,930 So, cannot concatenate string and integer, and invalid literal for int. 491 00:28:52,930 --> 00:28:55,140 It's trying to be as helpful as it possibly can 492 00:28:55,140 --> 00:28:57,710 be to give you a clue as to what to fix. 493 00:28:57,710 --> 00:29:00,270 So, again, not scolded. 494 00:29:02,030 --> 00:29:05,610 Okay, so that's variables and types and type conversion. 495 00:29:05,610 --> 00:29:09,563 Now we'll talk a little bit about user input. 496 00:29:09,563 --> 00:29:15,360 And there's a function that's built into Python called raw_input. 497 00:29:15,360 --> 00:29:21,080 And what happens when raw_input runs is it, it has as one of 498 00:29:21,080 --> 00:29:25,310 its parameters, a prompt, which is something that shows up on the screen. 499 00:29:25,310 --> 00:29:26,020 Who are you? 500 00:29:26,020 --> 00:29:27,820 And then, 501 00:29:27,820 --> 00:29:34,520 it waits, tik, tik, tik, tik, tik. Sits and waits, says, what next? 502 00:29:34,520 --> 00:29:36,920 And then, you type a string, dot, dot, dot, dot, dot, 503 00:29:36,920 --> 00:29:38,740 and then you hit the Enter key. 504 00:29:40,540 --> 00:29:41,640 The Enter key. 505 00:29:41,640 --> 00:29:47,310 And then whatever you typed here goes into a variable. 506 00:29:48,510 --> 00:29:52,650 And it is a string. And, then you, 507 00:29:52,650 --> 00:29:53,656 then you can use it. 508 00:29:53,656 --> 00:29:55,670 So I'm going to print the string Welcome, 509 00:29:55,670 --> 00:29:58,710 comma. So that means I'm printing two things now. 510 00:29:58,710 --> 00:30:01,740 The comma adds a space between Welcome and then nam, and so 511 00:30:01,740 --> 00:30:07,160 Welcome is a literal, and then Chuck is coming from this nam variable. 512 00:30:07,160 --> 00:30:09,380 So this is a two-line program. 513 00:30:09,380 --> 00:30:12,430 I think this is one of your assignments, actually, 514 00:30:12,430 --> 00:30:15,930 to well, it's one of the exercises in the book. 515 00:30:15,930 --> 00:30:17,840 To a prompt for a user's name, and 516 00:30:17,840 --> 00:30:19,550 then welcome them, okay? 517 00:30:21,382 --> 00:30:26,380 So raw_input is a function that issues a prompt, waits, and then takes whatever 518 00:30:26,380 --> 00:30:29,670 string that's entered, and then returns it and then puts it into that variable. 519 00:30:33,390 --> 00:30:38,190 So, now we're going to create kind of the first useful program. 520 00:30:38,190 --> 00:30:41,160 It's not a powerful program. 521 00:30:41,160 --> 00:30:48,630 It is a, an interesting problem of the fact that for some reason there 522 00:30:48,630 --> 00:30:50,190 is a difference in the numbering scheme 523 00:30:50,190 --> 00:30:53,140 of United States elevators and European elevators. 524 00:30:54,180 --> 00:30:58,440 European elevators, the floor that you walk out on is the 525 00:30:58,440 --> 00:30:59,750 zero floor. 526 00:30:59,750 --> 00:31:01,700 The floor above that is the one floor and the 527 00:31:01,700 --> 00:31:06,300 floor below that, the basement, is the minus one floor. 528 00:31:06,300 --> 00:31:11,198 And so you walk in and you can go either up the elevator or down the elevator. 529 00:31:11,198 --> 00:31:15,240 Of course, in the United States, the floor that you walk in is the 530 00:31:15,240 --> 00:31:20,120 one and then there's the two floor above that and then there's like, the basement. 531 00:31:20,120 --> 00:31:23,460 So this is the imagination that the Americans 532 00:31:23,460 --> 00:31:26,230 have as to how to number floors, right? 533 00:31:26,230 --> 00:31:29,290 The Europeans go zero, one, minus one. 534 00:31:29,290 --> 00:31:35,100 So, children who go to hotels learn instantly the notion of zero and the 535 00:31:35,100 --> 00:31:36,790 notion of positive and negative numbers and 536 00:31:36,790 --> 00:31:39,280 the symmetry between positive and negative numbers. 537 00:31:39,280 --> 00:31:44,520 I mean, I just wish the United States hotels would switch to this 538 00:31:44,520 --> 00:31:49,000 to teach young people zero immediately and 539 00:31:49,000 --> 00:31:50,190 negative numbers. 540 00:31:50,190 --> 00:31:53,730 So we somehow think that numbers all in the United States start at 1 541 00:31:53,730 --> 00:31:57,030 and then there are no negative numbers, there's the 542 00:31:57,030 --> 00:31:57,530 basement. 543 00:32:00,100 --> 00:32:01,990 I wonder why that is, but whatever. 544 00:32:03,630 --> 00:32:07,150 For people who travel a lot, they may be confused by this. 545 00:32:08,190 --> 00:32:09,780 They need a way to convert back and 546 00:32:09,780 --> 00:32:14,009 forth between the US and European numbering system. 547 00:32:15,500 --> 00:32:18,170 So this is a simple program that demonstrates 548 00:32:18,170 --> 00:32:22,160 a real classic pattern of input processing and output. 549 00:32:22,160 --> 00:32:25,380 It's just three lines, but it has the 550 00:32:25,380 --> 00:32:28,940 essential things that all programs that are useful. 551 00:32:28,940 --> 00:32:32,600 They generally read some data, do some work with 552 00:32:32,600 --> 00:32:36,070 the data, and then produce some kind of results. 553 00:32:36,070 --> 00:32:40,740 And so, so the first line is a raw_input 554 00:32:40,740 --> 00:32:45,240 that effectively, that puts out a prompt and then it waits. 555 00:32:45,240 --> 00:32:49,190 It says, please enter your Europe floor. It sits there. 556 00:32:49,190 --> 00:32:50,920 We type a zero, 557 00:32:50,920 --> 00:32:54,100 then zero goes into inp, but it is a string. 558 00:32:55,250 --> 00:32:56,303 It's not a number. 559 00:32:56,303 --> 00:32:57,700 It's a string. 560 00:32:57,700 --> 00:33:00,870 So we can't add to it. But we can take 561 00:33:02,100 --> 00:33:04,800 and convert it to an integer with the int function. 562 00:33:04,800 --> 00:33:07,710 Int of inp, thats a string being converted to an integer 563 00:33:07,710 --> 00:33:10,370 so now its a real numeric zero. 564 00:33:10,370 --> 00:33:14,200 And we can add 1 to that and we sum that together. 565 00:33:14,200 --> 00:33:16,210 And we put it into the 566 00:33:16,210 --> 00:33:20,800 variable usf and then we print US floor, comma, and then 567 00:33:20,800 --> 00:33:25,350 whatever the variable for usf is. And out comes US floor 1. 568 00:33:25,350 --> 00:33:29,280 So we've written a very simple elevator floor conversion 569 00:33:29,280 --> 00:33:32,100 from a European floor to a United States floor. 570 00:33:33,310 --> 00:33:35,810 Don't ask about negative numbers, it's not really good at that. 571 00:33:35,810 --> 00:33:39,120 But from zero and positive numbers it works great. 572 00:33:42,710 --> 00:33:48,130 So another thing to think about in any programming language is comments. 573 00:33:49,580 --> 00:33:54,410 Comments are like commentary, you know, and basically it's a way for us to 574 00:33:55,880 --> 00:34:00,410 add notations for ourselves and for other humans interspersed in the code. 575 00:34:01,480 --> 00:34:06,680 And so in Python anything after a pound sign is ignored. 576 00:34:06,680 --> 00:34:07,900 You can have a pound sign at the beginning 577 00:34:07,900 --> 00:34:09,545 of a line and then the whole line is ignored. 578 00:34:09,545 --> 00:34:12,670 There are two or three reasons why you could do this. 579 00:34:12,670 --> 00:34:15,700 One is sort of like paragraph headings, where you can 580 00:34:15,700 --> 00:34:21,190 say what's going to happen in English or, or your language. 581 00:34:21,190 --> 00:34:23,010 And you can write documentation says this 582 00:34:23,010 --> 00:34:27,510 code was written by Charles Severence, December 2010. 583 00:34:27,510 --> 00:34:29,800 And you can also just hide a line of code to 584 00:34:29,800 --> 00:34:32,920 test and, and turn it on and off without actually deleting 585 00:34:32,920 --> 00:34:36,900 the line of code. It's a real common thing in debugging. 586 00:34:36,900 --> 00:34:43,070 So for example, here is a, here is a, the program that we've been playing with. 587 00:34:43,070 --> 00:34:45,160 This is our word counting program that 588 00:34:45,160 --> 00:34:46,960 we've been talking about from the beginning. 589 00:34:46,960 --> 00:34:51,850 And here is an example of four comments, one, two, three, four. 590 00:34:51,850 --> 00:34:56,330 Four comments that basically tell us what these paragraphs are going to do. 591 00:34:56,330 --> 00:34:59,250 Now, they don't have any effect on the program whatsoever. 592 00:34:59,250 --> 00:35:01,150 But this one says get the name of the file and open it. 593 00:35:02,290 --> 00:35:03,860 Kind of helpful, right? 594 00:35:03,860 --> 00:35:04,910 Count the word frequency. 595 00:35:04,910 --> 00:35:07,870 That's what this little bit does. Find the most common word. 596 00:35:07,870 --> 00:35:09,490 That's what this little bit does. 597 00:35:09,490 --> 00:35:11,500 And all done, print this out. 598 00:35:11,500 --> 00:35:16,450 So it's really can be very helpful just to add a little tiny bit of stuff. 599 00:35:16,450 --> 00:35:18,410 And you don't want to overuse comments. 600 00:35:18,410 --> 00:35:23,470 You don't want to say like x equals 12, take 12 and put it into x. 601 00:35:23,470 --> 00:35:24,530 Sometimes people teach 602 00:35:24,530 --> 00:35:28,360 you and try to say, oh you need a one comment every three lines. 603 00:35:28,360 --> 00:35:29,760 I don't believe in any of those rules. 604 00:35:29,760 --> 00:35:33,230 I basically say if its useful to describe it, then describe it. 605 00:35:34,920 --> 00:35:41,100 So that's comments. So some operators apply to strings. 606 00:35:41,100 --> 00:35:43,410 We've already talked about plus. 607 00:35:43,410 --> 00:35:45,600 It's kind of silly, although useful in places. 608 00:35:45,600 --> 00:35:49,660 You can actually multiply strings. The asterisk looks and 609 00:35:49,660 --> 00:35:54,170 says I've got a string and an integer, and it prints out the string five times. 610 00:35:55,300 --> 00:35:56,120 Not a lot of use for that. 611 00:35:57,620 --> 00:36:01,420 Now, let's talk a little bit about choosing variable names. 612 00:36:01,420 --> 00:36:04,280 This is something that is really confusing. 613 00:36:04,280 --> 00:36:08,170 So I said like, x equals 1, x equals x plus 1, what does x mean? 614 00:36:08,170 --> 00:36:12,190 And the answer is, it doesn't mean anything. 615 00:36:12,190 --> 00:36:14,970 I chose it. I wanted to make a variable, 616 00:36:14,970 --> 00:36:16,770 and so I picked x. 617 00:36:16,770 --> 00:36:19,100 We pick x a lot, probably because we learned 618 00:36:19,100 --> 00:36:23,210 in algebra in sixth grade that x was a variable. 619 00:36:23,210 --> 00:36:26,270 So, and it's short, and so, why not call it x? 620 00:36:29,400 --> 00:36:32,610 But as your programs get larger this gets kind of frustrating 621 00:36:32,610 --> 00:36:35,510 to have all your variables like x and y and z. 622 00:36:35,510 --> 00:36:39,120 And so the notion of mnemonic, it means memory aid. 623 00:36:39,120 --> 00:36:44,150 We choose our variable names wisely, so they remind us of what the variable's 624 00:36:44,150 --> 00:36:49,360 going to do internally. And so, as I go through this lecture 625 00:36:51,540 --> 00:36:55,310 in the beginning if I choose a variable that's too clever 626 00:36:55,310 --> 00:36:59,050 you're going to think that it's part of the language. 627 00:36:59,050 --> 00:37:02,560 And so I sort of switch back and forth between well-chosen variable names 628 00:37:02,560 --> 00:37:07,055 and stupid variable names to kind of reemphasize the notion that I can choose. 629 00:37:07,055 --> 00:37:10,140 Mnemonic is a good practice, okay? 630 00:37:10,140 --> 00:37:13,860 So here we go. Let's take a look at a bit of code. 631 00:37:17,210 --> 00:37:21,100 So the question is, what is this code doing? 632 00:37:21,100 --> 00:37:23,290 What will it even print out? 633 00:37:23,290 --> 00:37:24,890 Is it syntactically correct? 634 00:37:27,380 --> 00:37:33,330 Now you could probably cut and paste this into your brow, into Python and figure 635 00:37:33,330 --> 00:37:39,266 out that it is syntactically correct. There are three variables. 636 00:37:41,266 --> 00:37:46,670 This one here and this one here match. 637 00:37:47,800 --> 00:37:52,860 This one here and that one there match. And these two match. 638 00:37:53,860 --> 00:37:55,360 So it's taking these two numbers and 639 00:37:55,360 --> 00:37:57,890 multiplying together, and then printing out the product 640 00:37:57,890 --> 00:38:03,790 of the two numbers, if you're real careful and like look at every, every character. 641 00:38:03,790 --> 00:38:07,180 Now, this would be called non-mnemonic variables. 642 00:38:07,180 --> 00:38:09,200 They're really messy. 643 00:38:09,200 --> 00:38:13,290 Now Python, it's happy, because all it wants is to say, oh. 644 00:38:13,290 --> 00:38:13,800 Here is then name that 645 00:38:13,800 --> 00:38:16,550 I, the programmer, decided I wanted to call this 646 00:38:16,550 --> 00:38:20,470 piece of memory and I'll refer to it down here, okay? 647 00:38:20,470 --> 00:38:23,320 And so Python is happy. 648 00:38:23,320 --> 00:38:27,280 Now if you hand this to another human being they are going to be really unhappy. 649 00:38:27,280 --> 00:38:29,170 Because they are going to be like, what are you doing? 650 00:38:30,270 --> 00:38:35,660 So one better way to write it would be to make the variables very simple. 651 00:38:35,660 --> 00:38:39,134 And then cognitively we humans can figure out which is which, 652 00:38:39,134 --> 00:38:42,720 because again it's still only about matching. 653 00:38:42,720 --> 00:38:48,470 The a has to match the a, the b matches the b, and the c's match. 654 00:38:48,470 --> 00:38:50,290 It's actually the exact same program. 655 00:38:50,290 --> 00:38:53,570 A equals 35. B equals 12.5. 656 00:38:53,570 --> 00:38:54,820 C equals A times B. 657 00:38:54,820 --> 00:38:57,740 And print C. It is these. 658 00:38:57,740 --> 00:39:00,980 Python sees these as the same program. 659 00:39:00,980 --> 00:39:04,210 It doesn't care what we name them. Now, a human will 660 00:39:04,210 --> 00:39:08,770 be much appreciative if you say, here you can either have this one or this one. 661 00:39:08,770 --> 00:39:11,070 This one will make them a lot happier. 662 00:39:13,450 --> 00:39:14,480 Okay? 663 00:39:14,480 --> 00:39:18,610 So that is certainly cognitively easier, but it's not really 664 00:39:18,610 --> 00:39:23,240 giving you any sense of what's going on here, right? 665 00:39:23,240 --> 00:39:29,260 So an even better way to write this exact same program to do the exact same thing 666 00:39:29,260 --> 00:39:32,430 would be to choose variables named hours, rate, and pay, 667 00:39:32,430 --> 00:39:35,810 if indeed that is what you're doing. 668 00:39:35,810 --> 00:39:38,610 Now you can look at this and you go, oh well, shoot, 669 00:39:38,610 --> 00:39:42,730 35 is the number of hours, and 12.5 is the rate, and the pay is 670 00:39:42,730 --> 00:39:46,090 the number of hours times the rate, and then we are going to print out the pay. 671 00:39:46,090 --> 00:39:48,060 And that makes a lot of sense. 672 00:39:48,060 --> 00:39:54,290 So this is really a awesome and wonderful characterization. 673 00:39:54,290 --> 00:39:56,592 And if that's what you're doing and those are hours, 674 00:39:56,592 --> 00:40:00,090 rate, and pay, it's a great thing to name the variables. 675 00:40:00,090 --> 00:40:04,370 But, this is where beginning students get confused. 676 00:40:04,370 --> 00:40:07,415 And so sometimes I'll write it this way and sometimes I'll write it this way. 677 00:40:07,415 --> 00:40:09,749 Because you'll look at this, until you get a little 678 00:40:09,749 --> 00:40:12,880 more sophisticated, a little more skilled, and you'll say like 679 00:40:15,180 --> 00:40:19,430 does Python know something about payroll? Is hours a reserved word? 680 00:40:19,430 --> 00:40:22,370 Is rate a reserved word and pay a reserved word? 681 00:40:22,370 --> 00:40:26,350 Are these things that Python knows about? And the answer is, no. 682 00:40:26,350 --> 00:40:30,270 Python sees these three programs as exactly the same name. 683 00:40:30,270 --> 00:40:34,440 It's just this person really made a very bad choice of variable name. 684 00:40:34,440 --> 00:40:37,740 This person made a less bad choice of variable name, 685 00:40:37,740 --> 00:40:40,830 and this person made a really awesome choice of variable name. 686 00:40:40,830 --> 00:40:43,010 So the only difference between those two things is style. 687 00:40:45,100 --> 00:40:47,260 They are the exact same program. 688 00:40:47,260 --> 00:40:51,460 And Python is equivalently happy with these, but humans 689 00:40:51,460 --> 00:40:55,280 are most happy when the variables are easy to remember 690 00:40:55,280 --> 00:40:58,840 and they are somewhat descriptive of what their expected contents will be. 691 00:40:59,840 --> 00:41:01,530 That's mnemonic. 692 00:41:01,530 --> 00:41:05,680 To help you remember what you were meaning to do when you write the program. 693 00:41:05,680 --> 00:41:08,280 This is still a bit cryptic, having these 694 00:41:08,280 --> 00:41:10,340 really short, one-character variable names is still 695 00:41:10,340 --> 00:41:12,320 a bit cryptic. So, 696 00:41:14,460 --> 00:41:17,220 You have a couple of assignments at the end of the chapter. 697 00:41:17,220 --> 00:41:20,740 One of the assignments is to write a program to prompt 698 00:41:20,740 --> 00:41:25,220 the user for hours and rate per hour and compute pay. 699 00:41:26,430 --> 00:41:32,566 So, I won't do this here, but just a couple of sort of odd things. 700 00:41:32,566 --> 00:41:34,755 You're going to be using raw_input. 701 00:41:34,755 --> 00:41:39,528 But remember that hands a string in so you're going 702 00:41:39,528 --> 00:41:41,980 to have to use float. 703 00:41:44,030 --> 00:41:45,690 The function to convert it to a floating 704 00:41:45,690 --> 00:41:47,755 point number so you can actually do a calculation. 705 00:41:47,755 --> 00:41:51,170 And then you're going to have to use multiplication and print. 706 00:41:51,170 --> 00:41:53,210 So then multiplication, and then print. 707 00:41:55,200 --> 00:41:59,540 So some combination of raw input, float, multiplication, and print, 708 00:42:00,790 --> 00:42:04,170 constructed to make your program do exactly this. 709 00:42:05,690 --> 00:42:08,350 So, this is the end of Chapter Two. 710 00:42:08,350 --> 00:42:09,210 We talked about types, 711 00:42:09,210 --> 00:42:14,400 reserved words, variables, the mnemonic, how you choose variable names. 712 00:42:14,400 --> 00:42:15,835 We'll hit this a couple more times 713 00:42:15,835 --> 00:42:18,400 because choosing variable names is always problematic. 714 00:42:18,400 --> 00:42:22,400 Operators, operator precedence, which just means like does multiplication happen 715 00:42:22,400 --> 00:42:26,980 before plus, parentheses. Integer division is a little weird because 716 00:42:26,980 --> 00:42:34,255 it truncates, oops, right, 9 over 10. 717 00:42:34,255 --> 00:42:41,050 Oops, 9 over 10 equals 0. That's the integer division truncating. 718 00:42:41,050 --> 00:42:47,930 Conversion, this is like the int() float(). 719 00:42:47,930 --> 00:42:50,030 And then user input, which is raw_input. 720 00:42:50,030 --> 00:42:52,340 And then comments, which are ways for you 721 00:42:52,340 --> 00:42:55,690 to add human-readable text to your program. 722 00:42:55,690 --> 00:42:57,560 Okay? See you next lecture.