0:00:00.420,0:00:04.560 Hello, and welcome to Chapter Two.[br]Hope you enjoyed Chapter One. 0:00:04.560,0:00:08.950 It was one of the longer lectures.[br]Trying to motivate you a little bit. 0:00:08.950,0:00:12.050 And now we're going to kind of go back to[br]the basics, to the, chapter 0:00:12.050,0:00:15.950 Chapter One covered sort of the first four[br]to five chapters of the book. 0:00:15.950,0:00:20.140 So as always, this this video,[br]these slides are 0:00:20.140,0:00:23.620 copyright Creative Common Attribution, as[br]well as the audio. 0:00:24.810,0:00:25.380 And so, 0:00:26.390,0:00:28.430 now we're going to talk about[br]sort of the really 0:00:28.430,0:00:32.876 low-level things that make up the Python[br]language. 0:00:32.876,0:00:36.533 Constants. So I'm going to summarize this[br]terminology just so I 0:00:36.533,0:00:39.950 can like say the word "constant" and you[br]won't freak out. 0:00:39.950,0:00:45.590 A constant is as contrasted with something[br]that changes, a variable. 0:00:45.590,0:00:48.840 We talk about variables in the next slide.[br]But for now, constants. 0:00:48.840,0:00:51.480 Constants are in things that are sort 0:00:51.480,0:00:54.150 of natural and instinctive.[br]Things like numbers. 0:00:54.150,0:00:56.656 A hundred and twenty-three. 0:00:56.656,0:00:59.170 98.6, or Hello world. 0:00:59.170,0:01:02.600 And so in, in, what, what I'm doing here[br]is we're, we're using 0:01:02.600,0:01:06.150 a Python interpreter, and that, that's how[br]you can tell, the chevron prompt. 0:01:06.150,0:01:10.296 And I'm saying print 123, and then Python[br]responds with 123, 0:01:10.296,0:01:16.420 print 98.6, Python responds with 98.6,[br]and print 'Hello world'. 0:01:16.420,0:01:21.305 So the constants are the 123, 98.6, and[br]'Hello world'. 0:01:21.305,0:01:22.770 So these are things. 0:01:22.770,0:01:26.930 We can use either single quotes or double[br]quotes to make strings. 0:01:26.930,0:01:29.730 And so programs kind of work with numbers[br]and work with 0:01:29.730,0:01:34.720 strings and we have these non-varying[br]values that we call constants. 0:01:34.720,0:01:39.940 So the other side of the picture is a[br]variable. 0:01:39.940,0:01:42.070 And the way I like to characterize a[br]variable 0:01:42.070,0:01:44.740 is it's a place in the memory of the[br]computer. 0:01:46.100,0:01:47.590 We give it a name as a programmer. 0:01:47.590,0:01:49.530 We pick the variable name. 0:01:49.530,0:01:55.130 In this, I'm saying x equals 12.2 and y[br]equals 14. 0:01:55.130,0:01:58.490 I am choosing the name and I'm choosing[br]what to put in there. 0:01:59.610,0:02:04.040 This is a statement called an assignment[br]statement, and the way to 0:02:04.040,0:02:07.290 think of the assignment statement is that[br]it sort of has a direction. 0:02:08.370,0:02:12.010 We're saying, dear Python, go find some[br]memory. 0:02:12.010,0:02:15.780 I will use label x later to, to refer to[br]that 0:02:15.780,0:02:19.680 memory, and take the number 12.2 and stick[br]it into x. 0:02:19.680,0:02:21.460 Then this is sequential code. 0:02:21.460,0:02:24.360 Then the next thing I want you to do is[br]I'd like you to go find some 0:02:24.360,0:02:30.950 more memory, call it y, I will call it y[br]later, and stick 14 in there, okay? 0:02:30.950,0:02:34.280 And so that ends up sort of with two[br]little areas 0:02:34.280,0:02:34.800 of memory. 0:02:36.500,0:02:38.950 You know, the one labeled x, and [br]here's a 0:02:38.950,0:02:41.930 little cell in which we, like a drawer, or[br]something. 0:02:41.930,0:02:44.960 And one labeled y. And we put 12.2.[br]After these 0:02:44.960,0:02:49.550 lines run, we have 12.2 in one and [br]14 in the other. 0:02:49.550,0:02:55.050 Then, for example, if there's another line[br]that's down here, so there's this 0:02:55.050,0:02:59.250 third line after this has happened, after[br]this has happened, x equals 100. 0:02:59.250,0:03:02.300 Remember, this has kind of got an, a[br]direction to it, see? 0:03:02.300,0:03:07.460 Oh, remember that x that I had, you know,[br]I would like now to put 100 in that. 0:03:07.460,0:03:09.770 So as I'm thinking this through, I think[br]of that as sort of 0:03:09.770,0:03:14.920 removing the 12.2 or overwriting the 12.2[br]and putting 100 in its place. 0:03:14.920,0:03:20.830 And so at the end here, x is left with 100[br]and y is left with 1 4 with 14. 0:03:20.830,0:03:24.510 So these variables can kind of have one[br]value in them and 0:03:24.510,0:03:26.100 what we can look at them and we can 0:03:26.100,0:03:28.850 reuse them and put different values in if[br]we want. 0:03:30.900,0:03:33.870 There are some rules for naming your[br]variables. 0:03:33.870,0:03:35.840 Again, you get to pick the variable names. 0:03:37.290,0:03:39.530 Often we pick variables that make some[br]sense. 0:03:39.530,0:03:41.560 We'll talk about that in a second. 0:03:41.560,0:03:44.900 In Python variables can start with an[br]underscore. 0:03:44.900,0:03:48.090 We tend not to, as normal programmers, use[br]those. 0:03:48.090,0:03:52.330 We let libraries use those. 0:03:52.330,0:03:54.880 It has to have letters, numbers, and[br]underscores. 0:03:54.880,0:03:56.230 And, and start 0:03:56.230,0:04:00.030 with start with a letter or an underscore. 0:04:00.030,0:04:05.600 Case matters, so spam is good, eggs is 0:04:05.600,0:04:08.300 good, spam23 is good because [br]the number is not 0:04:08.300,0:04:11.860 the first character, _speed, that's also[br]perfectly fine 0:04:11.860,0:04:13.605 because it start with an [br]underscore or a letter. 0:04:13.605,0:04:20.839 [COUGH] 23Spam starts with a letter, [br]starts with a number, so that's bad. 0:04:20.839,0:04:21.290 This starts with something 0:04:21.290,0:04:24.390 other than a letter or an underscore. 0:04:24.390,0:04:28.050 And you can't use a dot in [br]the variable name. 0:04:28.050,0:04:31.280 It turns out the dot has meaning to Python[br]that would confuse it. 0:04:34.130,0:04:37.080 That would confuse it and wouldn't[br]understand [COUGH] what we 0:04:37.080,0:04:39.150 really mean there, and so that would be a[br]syntax error. 0:04:39.150,0:04:41.030 That would be a syntax error. 0:04:41.030,0:04:45.850 Because case is sensitive, that means that[br]things like all lowercase 0:04:45.850,0:04:49.720 spam is different than a upper case S and[br]all uppercase. 0:04:49.720,0:04:54.990 These are three distinct variables that[br]are unique. 0:04:54.990,0:04:58.590 Most people don't use, choose variables[br]that might be so confusing. 0:04:58.590,0:04:59.440 So that's to 0:04:59.440,0:05:01.860 you as you write it and as to anybody that 0:05:01.860,0:05:06.400 might read it would find three variables[br]named this very confusing. 0:05:06.400,0:05:08.060 So it's a bad idea. 0:05:08.060,0:05:10.160 Don't do it, but I'm just showing you as 0:05:10.160,0:05:14.900 an example that case can make a variable[br]name distinct. 0:05:14.900,0:05:18.170 And again, this variable is a place in[br]memory 0:05:18.170,0:05:22.400 that we are going to store and retrieve[br]information. 0:05:22.400,0:05:24.680 Whether that be numbers or strings or[br]whatever. 0:05:24.680,0:05:25.840 These are things that we control. 0:05:27.010,0:05:30.160 Now Python also has a set of reserved[br]words. 0:05:30.160,0:05:33.040 What it really means is you can't use[br]these for variables. 0:05:33.040,0:05:38.854 These words have very special meaning.[br]And, for, is, raise, if. 0:05:38.854,0:05:44.610 So you can't make a variable named i-f.[br]It would be like, oh no, that is "if". 0:05:44.610,0:05:45.470 I know what "if" is. 0:05:45.470,0:05:49.350 So these are words that Python has as its[br]core vocabulary. 0:05:49.350,0:05:51.180 And forbids you to use them 0:05:51.180,0:05:55.610 for other purposes, like[br]variable names or later function names. 0:05:55.610,0:06:03.000 So that's kind of the vocabulary.[br]Constants, variables, and reserved words. 0:06:03.000,0:06:05.990 Now, we take these and we start assembling[br]them 0:06:05.990,0:06:10.580 into sort of sentences, statements, Python[br]statements that do something. 0:06:10.580,0:06:13.290 So we've already talked about an[br]assignment statement. 0:06:13.290,0:06:15.200 It has kind of an arrow here. 0:06:15.200,0:06:18.210 It says, hey Python, go find me a place[br]called x. 0:06:18.210,0:06:22.710 Take the number 2 and stick it in there[br]for later, then continue on. 0:06:22.710,0:06:26.990 Now, because there's an arrow,[br]the right side of this is done first. 0:06:26.990,0:06:31.360 And so it said, so this right side, you[br]can kind of ignore for the moment the 0:06:31.360,0:06:33.410 left-hand side and it calculates the [br]right-hand 0:06:33.410,0:06:35.530 side by looking at the current [br]value for x. 0:06:35.530,0:06:40.620 Which happens to be 2, and adds these[br]two things together, and then gets 4. 0:06:40.620,0:06:44.910 And then, at the point where it knows[br]4, that this 0:06:44.910,0:06:48.740 number is 4, it will then store that[br]back into X. 0:06:48.740,0:06:53.790 And so then, later, we print x and we will[br]get the 4. And so again, this is 0:06:53.790,0:06:57.180 a sequence of steps and the, the [br]variable x 0:06:57.180,0:07:00.800 changes as these steps continue.[br]And when we're saying print x, 0:07:00.800,0:07:03.720 that really means print the [br]current value for x. 0:07:07.630,0:07:12.580 So, we can do a number of different[br]operators and assignment statements. 0:07:12.580,0:07:15.960 We calculate this right-hand side. 0:07:15.960,0:07:19.170 This is sort of all calculated, whatever[br]this is, based on 0:07:19.170,0:07:22.450 the current value for x does this[br]calculation, and then when 0:07:22.450,0:07:25.570 it knows what the answer is, it assigns[br]that into the 0:07:25.570,0:07:28.586 variable that's on the left-hand side of[br]the assignment statement. 0:07:28.586,0:07:32.730 Again, calculate the right-hand 0:07:32.730,0:07:35.670 side completely and then move it to the[br]left-hand side. 0:07:35.670,0:07:38.790 Some early languages actually didn't use 0:07:38.790,0:07:41.280 the equals sign for the assignment[br]operator. 0:07:41.280,0:07:46.390 This assignment operator in, in a way it[br]kind of [INAUDIBLE] 0:07:46.390,0:07:46.790 Some languages 0:07:46.790,0:07:50.750 An early language actually[br]used an arrow. 0:07:50.750,0:07:53.110 Arrows aren't really on people's[br]keyboards. 0:07:53.110,0:07:57.950 Another language used colon equals as[br]this assignment operator. 0:07:57.950,0:07:58.890 But we use equals. 0:07:58.890,0:08:03.800 Now, if you're familiar with math this can[br]be a little confusing, like x equals 1 0:08:03.800,0:08:07.780 and then X equals 2. That as mathematics[br]would be bad math 0:08:07.780,0:08:11.580 because in a proof or a problem, x can only[br]have one value. 0:08:11.580,0:08:14.740 But in programming if this was two[br]statements, that means 0:08:14.740,0:08:17.935 just x had a value, and then the value[br]for x changed later. 0:08:17.935,0:08:23.150 Okay. So just kind of go through this[br]because it's 0:08:23.150,0:08:27.090 working from the right-hand side to the[br]left-hand side on assignment statements. 0:08:27.090,0:08:30.940 It is pulling out these x values, so x may[br]have 0.6. 0:08:30.940,0:08:35.679 It pulls the values out before, sort of[br]ignoring this part 0:08:35.679,0:08:39.280 right here, and it's just going to try to[br]resolve this expression. 0:08:39.280,0:08:42.570 And it has multiplication and parentheses[br]and things like that. 0:08:42.570,0:08:46.480 So it basically pulls the 0.6 into the[br]calculation, 0:08:46.480,0:08:48.610 does the 1 minus x, which gives you 0.4. 0:08:48.610,0:08:52.660 Then it multiplies these three things[br]together, giving 0.93. 0:08:52.660,0:08:56.904 And then when it is all done with all of[br]that, it takes that. 0:08:56.904,0:08:57.270 Oops. 0:08:57.270,0:09:03.980 It takes that 0.93, and then puts it back[br]into x. 0:09:03.980,0:09:08.230 And so this is just sort of emphasizing[br]how the right-hand side is computed to 0:09:08.230,0:09:13.630 produce a value, then it is moved into the[br]variable, and that is why you 0:09:13.630,0:09:15.995 can have sort of x on both sides. 0:09:15.995,0:09:20.140 Because this is like the old, and this is[br]the new. 0:09:20.140,0:09:23.690 This is the old x participates in the[br]calculation, and 0:09:23.690,0:09:27.720 then when the calculation is done, it[br]becomes the new x. 0:09:27.720,0:09:28.410 Hope that makes sense. 0:09:29.500,0:09:32.980 So, this, on the right-hand side here is[br]a numeric expression. 0:09:32.980,0:09:35.650 So we have a number of different[br]operators. 0:09:35.650,0:09:38.760 Some of them are instinctive, [br]intuitive. 0:09:38.760,0:09:40.260 The plus and the minus. 0:09:40.260,0:09:43.310 The reason some of these are so weird is[br]in the really old days, we 0:09:43.310,0:09:45.950 didn't have too many things on the[br]keyboard, 0:09:45.950,0:09:48.100 and a lot of programs were very[br]mathematical. 0:09:48.100,0:09:49.410 And so they figured out what was on 0:09:49.410,0:09:52.220 the keyboard of the computer equipment of[br]the day. 0:09:52.220,0:09:55.110 And then they had to fake certain things. 0:09:55.110,0:09:57.750 So, it turns out that plus and minus [br]were on the keyboard, 0:09:57.750,0:10:02.620 and so plus and minus are[br]addition and subtraction, respectively. 0:10:02.620,0:10:04.140 There was no kind of times 0:10:04.140,0:10:08.360 operator for multiplication, and dot was[br]used for decimal points. 0:10:08.360,0:10:12.930 So they used asterisk for multiplication.[br]So on computers' languages, nearly 0:10:12.930,0:10:17.760 all of them, they basically use a mult[br]times for multiplication. 0:10:17.760,0:10:19.790 Slash is used for division. 0:10:19.790,0:10:23.460 So we say like, 8/2, which is 8 divided [br]by 2. 0:10:24.642,0:10:29.727 Raising something to the power like 4[br]squared, 0:10:29.727,0:10:35.680 that is double asterisk.[br]And then remainder is if you 0:10:35.680,0:10:40.480 do a division that gives you the remainder[br]rather than divisor. 0:10:40.480,0:10:44.580 So 8 over 2 is 4 remainder 0. So 0:10:44.580,0:10:48.100 the remainder is what you get with this[br]particular operator. 0:10:48.100,0:10:49.900 There's a few cool things that we can do 0:10:49.900,0:10:52.610 with remainder that we won't talk about[br]right away. 0:10:52.610,0:10:54.660 But it's there. 0:10:54.660,0:10:57.470 And so here's just a couple of sample[br]expressions. 0:10:58.888,0:11:03.270 That's giving me green. 0:11:03.270,0:11:04.240 Okay. 0:11:04.240,0:11:06.690 So, so again, I'm using a Python[br]Interpreter. 0:11:06.690,0:11:08.310 So you can kind of, this is just the[br]prompt. 0:11:08.310,0:11:10.750 These chevrons are the prompt. 0:11:10.750,0:11:13.760 Create the variable xx, and [br]assign it to 2. 0:11:13.760,0:11:16.580 Retrieve the old value and an addition. 0:11:16.580,0:11:20.050 Then print it out and put it back into xx[br]so xx 0:11:20.050,0:11:24.110 has 4.[br]yy, this is a multiplication, 440 times 12. 0:11:24.110,0:11:28.780 It is 5,280. yy over 1,000.[br]Now this is a little counter-intuitive 0:11:28.780,0:11:35.060 Because yy is an integer,[br]it then does it in a truncated division. 0:11:35.060,0:11:42.080 And so, 5,280 divided by 1000 is 5.[br]Now if, and, 0:11:42.080,0:11:45.160 and so that's an integer division.[br]We'll see in a second 0:11:45.160,0:11:46.490 about floating point division. 0:11:47.990,0:11:51.570 Now we take the variable jj and we set it[br]to 23. 0:11:51.570,0:11:55.860 And now we're going to use the modular or[br]modulo or remainder operator. 0:11:55.860,0:12:01.610 Say what is jj, what is the remainder when[br]divide this jj by 5. 0:12:01.610,0:12:04.710 And so if you think about this, we take[br]old long division, 0:12:04.710,0:12:09.187 23 divided by 5, you end up with 4 and[br]then remainder 3. 0:12:10.590,0:12:13.010 The modulo operator, or the percent of 0:12:13.010,0:12:16.090 the remainder operator, gives us back this[br]number. 0:12:16.090,0:12:18.670 And so that's why kk is 3. 0:12:18.670,0:12:22.780 It is the remainder of 23 when [br]divided by 5, 0:12:22.780,0:12:27.670 or the remainder of the division [br]of 5 into 23. 0:12:27.670,0:12:32.090 And the raising to the power, 4 cubed.[br]That's not so nice. 0:12:32.090,0:12:34.550 4 cubed is 4 star, star 3. 0:12:34.550,0:12:35.842 And so that ends up being 64. 0:12:35.842,0:12:42.400 So that's just operations.[br]Now, just like in algebra and mathematics 0:12:43.510,0:12:49.650 we have rules about when to which, which[br]operations happen first. 0:12:49.650,0:12:52.940 In general, things like the power happens[br]before the 0:12:52.940,0:12:56.030 multiplication and division, and then the[br]addition and subtraction happen. 0:12:56.030,0:12:58.630 And so there are some rules that, when 0:12:58.630,0:13:00.940 you're looking at an expression and trying[br]to calculate 0:13:00.940,0:13:05.910 what its value is, if you don't have[br]parentheses, it follows these rules. 0:13:05.910,0:13:10.120 And so the, the most, the rule [br]that sort of 0:13:10.120,0:13:14.430 trumps all the rules is that parentheses[br]are always respected. 0:13:14.430,0:13:16.328 So a lot of us just write these with 0:13:16.328,0:13:19.550 parentheses in place, even sometimes[br]though you don't need it. 0:13:21.100,0:13:24.930 Then after parentheses have been handled,[br]then it does exponentiation. 0:13:24.930,0:13:26.220 Then it does multiplication, 0:13:26.220,0:13:30.188 division, and remainder.[br]And then it does addition and subtraction. 0:13:30.188,0:13:33.960 And then, when all else being equal, it[br]just works left to right. 0:13:33.960,0:13:40.370 So let's, let's look through an example.[br]So here is a 0:13:40.370,0:13:47.250 calculation that is, you know, 1, 1 plus 2[br]times 3 divided 4 over 5. 0:13:47.250,0:13:51.380 And the question is, what order does this[br]happen, okay? 0:13:51.380,0:13:53.470 And so let's sort of take a look at this. 0:13:55.210,0:13:58.780 And so, we start with are there any[br]parentheses? 0:13:58.780,0:14:00.620 And the answer is no, there are no[br]parentheses. 0:14:00.620,0:14:05.100 So let's go next.[br]Power. 0:14:05.110,0:14:11.100 And so the, the power says okay, let's[br]look across and find those things that 0:14:11.100,0:14:17.380 are raised to a power. And 2 cubed or 2 to[br]the third power is the, the power. 0:14:17.380,0:14:20.080 So we're going to do that one.[br]Okay. 0:14:20.080,0:14:21.880 And then we can, the way I do it when 0:14:21.880,0:14:24.150 I'm sort of doing these slowly is I[br]rewrite it. 0:14:24.150,0:14:28.710 So the 2 to the third power becomes 8, so[br]it's 1 plus 8 over 4 times 5. 0:14:28.710,0:14:31.730 And then now we can say oh power, that's[br]taken care of. 0:14:31.730,0:14:35.540 Now we're going to do multiplication and[br]division and we go across. 0:14:35.540,0:14:38.620 Now we have both a division and[br]multiplication. 0:14:38.620,0:14:40.310 Okay? Multiplication and division are done[br]at the same 0:14:40.310,0:14:42.630 time, so that means we do left to right, 0:14:42.630,0:14:45.105 which means we do the first one we[br]encounter first. 0:14:45.105,0:14:53.500 And so that will be 8 over 4 because of[br]the left-to-right rule. 0:14:53.500,0:14:55.220 And so we find that one, and that's the 0:14:55.220,0:14:58.520 one that gets computed next, and that[br]turns into 2. 0:14:58.520,0:15:00.550 And again, I like to rewrite these[br]expressions 0:15:00.550,0:15:03.910 just to keep my brain really, really[br]clear. 0:15:03.910,0:15:06.860 After a while you just do it in your head,[br]but I rewrite them. 0:15:06.860,0:15:07.700 When I was first learning it, 0:15:07.700,0:15:09.070 at least, I rewrote it all the time. 0:15:10.620,0:15:15.480 And and so next looking at this, there's a[br]multiplication. 0:15:15.480,0:15:19.660 We're not done with multiplication yet.[br]So the 2 over 5 is the next thing. 0:15:21.040,0:15:24.890 And then we do that calculation, and that[br]becomes 10, and again we rewrite it. 0:15:24.890,0:15:28.670 And now we've done the multiplication, and[br]we're going to do addition next. 0:15:28.670,0:15:33.950 And that's just 1 over 10, and that[br]becomes 11. 0:15:33.950,0:15:36.540 And so basically, this big long thing, 0:15:36.540,0:15:40.700 through a series of successive steps,[br]becomes 11. 0:15:40.700,0:15:44.110 And indeed, when we print it out, that's[br]what we get. 0:15:44.110,0:15:44.610 Okay? 0:15:46.810,0:15:49.140 So, there's the rules that are[br]parentheses, 0:15:49.140,0:15:52.440 power, multiplication, addition, and then,[br]left to right. 0:15:52.440,0:15:58.710 But smart people usually just put[br]parentheses in, you know? 0:15:58.710,0:16:01.760 So here's this, here's an exam.[br]Oop, go back, go back. 0:16:01.760,0:16:03.360 Here's an exam question. 0:16:03.360,0:16:09.040 Now, I wouldn't write this code, right, I[br]wouldn't write this code this way. 0:16:09.040,0:16:11.270 I would put a parentheses here. 0:16:12.360,0:16:13.100 And a parentheses there. 0:16:14.600,0:16:18.920 It's the same thing because that's exactly[br]the 2 times 3 is going to happen and 0:16:18.920,0:16:20.800 4 over 5 is going to happen and then the 0:16:20.800,0:16:23.140 plus and the minus will happen [br]left to right. 0:16:23.140,0:16:25.930 But why not make it easier on your readers 0:16:25.930,0:16:28.430 and just put the parentheses in. Because[br]they're redundant. 0:16:28.430,0:16:31.460 They're not necessary, but away you go. 0:16:31.460,0:16:34.656 Now, if you don't want it to happen in[br]that order, of 0:16:34.656,0:16:38.110 course then you have to put parentheses if[br]you want the addition 0:16:38.110,0:16:40.890 to happen before the multiplication, then[br]you 0:16:40.890,0:16:43.190 have to put parentheses in, which you can. 0:16:43.190,0:16:48.500 But we tend to recommend that you use more[br]parentheses rather than less parentheses. 0:16:49.690,0:16:53.530 Now, Python integer division [br]in Python 2, 0:16:53.530,0:16:56.120 which we're using Python 2 [br]for this class. 0:16:56.120,0:17:00.090 There's a new Python 3 that the world is[br]slowly transitioning 0:17:00.090,0:17:03.060 to and a lot of people are [br]using it in teaching. 0:17:03.060,0:17:08.369 But it's not as common, sort of, in the[br]real world with libraries and utilities. 0:17:08.369,0:17:10.671 And so we'll stick with [br]Python 2 for a few 0:17:10.671,0:17:14.890 more years until Python 3 really kind of[br]turns the corner. 0:17:14.890,0:17:17.910 It's nice to have it there, but there's so[br]much Python and it's so 0:17:17.910,0:17:22.960 popular, Python 2, that it's just kind of[br]hard to get everybody up to Python 3. 0:17:22.960,0:17:28.900 So in Python 2, integer division truncates[br]and you saw that before where 0:17:28.900,0:17:34.060 I did the 5280 by 1000 and I got 5 as and,[br]and, but we 0:17:34.060,0:17:38.030 can look at a couple of examples that make[br]this really very quite, quite clear. 0:17:38.030,0:17:40.820 So, 10 divided by 2 is 5 as you would[br]expect. 0:17:40.820,0:17:42.960 9 Divided by 2 is 4. 0:17:42.960,0:17:44.630 Not exactly what you'd expect. 0:17:44.630,0:17:49.220 You kind of expect that to be 4.5, [br]instead of 4. 0:17:49.220,0:17:53.950 But in Python 3, it will be 4.5, but for[br]now, in Python 2, 0:17:53.950,0:18:00.920 9 over, 9 over 2 is 4.[br]And 99 over 100 is 0. 0:18:00.920,0:18:03.520 Now that seems rather counter-intuitive,[br]but it is a truncating 0:18:03.520,0:18:07.150 division, it's not a rounding division,[br]it's a truncating division. 0:18:07.150,0:18:10.980 Now, interestingly, if you make either of[br]these numbers have a decimal, make them 0:18:10.980,0:18:16.282 what we call floating point numbers, then[br]the division is done in floating point. 0:18:16.282,0:18:19.530 So, 10.0 over 2.0 0:18:19.530,0:18:24.090 is 5.0.[br]Now, these are different. 0:18:24.090,0:18:26.873 This is an integer number, and this is a[br]floating point number. 0:18:26.873,0:18:27.836 It's 5.0. 0:18:27.836,0:18:31.830 And then 99.0 over 100.0 is exactly as you 0:18:31.830,0:18:34.580 would expect, and it's a floating point[br]number, so. 0:18:36.830,0:18:41.310 Now you can also mix integers and floating[br]point numbers as you go. 0:18:41.310,0:18:43.430 So here we have 99 over 100. 0:18:43.430,0:18:46.890 Those are both integers.[br]Integer, integer. 0:18:46.890,0:18:50.010 And, or, and that comes out with 0 because[br]it's truncating. 0:18:50.010,0:18:53.080 Now if we have an integer and [br]a floating point 0:18:53.080,0:18:57.191 number, 99 over 100.0, then that comes out[br]as 0.99. 0:18:58.350,0:19:01.990 And either one, if we have 99 over 100,[br]that's a floating point, and 0:19:01.990,0:19:02.710 that's an integer. 0:19:02.710,0:19:06.520 We still end up with a floating point, so[br]this is a floating point, floating point. 0:19:06.520,0:19:11.320 And even in complex expressions, as it[br]evaluates when 0:19:11.320,0:19:13.270 it sees an integer, so the first thing[br]when 0:19:13.270,0:19:20.820 you evaluate is this would become a 6, so[br]it would be 1 plus 6 over 4.0 minus 5. 0:19:20.820,0:19:27.461 Then it would be doing the 6 over 4.0 and[br]that would be 1.5, 1 plus 1.5 0:19:27.461,0:19:30.900 minus 5. And so this is an integer [br]and that's 0:19:30.900,0:19:33.740 a floating point and the result becomes a[br]floating point. 0:19:33.740,0:19:36.530 And then the rest of the calculation is[br]done floating point 0:19:36.530,0:19:41.200 to the point where the ultimate is a[br]floating point negative 2.5. 0:19:41.200,0:19:45.260 So you can throw a floating point into a[br]calculation and as soon as the 0:19:45.260,0:19:48.290 calculation touches the floating point,[br]the remainder 0:19:48.290,0:19:50.840 of the calculation is done in [br]floating point. 0:19:50.840,0:19:52.644 It kind of converts at the floating point [br]but it doesn't 0:19:52.644,0:19:55.830 want to convert it back because it[br]considers floating 0:19:55.830,0:19:59.429 point sort of the more general of the[br]representations. 0:20:01.800,0:20:06.540 So, here we are, talking about integers[br]and floating points. 0:20:06.540,0:20:10.520 These are a concept in programming[br]languages and in Python called type. 0:20:11.820,0:20:14.270 Variables and constants have a type. 0:20:15.600,0:20:18.540 We can see that if you say 1, versus 1.0, 0:20:18.540,0:20:22.090 they have different, they, it works[br]different, it functions differently. 0:20:22.090,0:20:28.300 And so Python keeps track of both[br]variables and literals/constants, and 0:20:28.300,0:20:32.240 having them have a type.[br]And we've seen this, right? 0:20:32.240,0:20:34.920 Now, the interesting thing is, is Python[br]is very aware of 0:20:34.920,0:20:39.920 the type and can use the same syntax to[br]accomplish different things. 0:20:39.920,0:20:44.200 So if we look at this line here, where we[br]say dd equals 1 plus 4. 0:20:44.200,0:20:45.790 Well it looks at the 1 and looks at the 4[br]and it says, 0:20:45.790,0:20:48.770 oh those are two integers. I will add those[br]together and give you a 5. 0:20:48.770,0:20:53.210 So it gives you an integer, an integer, and an[br]integer comes back, 0:20:53.210,0:20:53.310 Okay? 0:20:53.310,0:20:57.820 And then ee equals 'hello ' plus 'there'.[br]Well these are two strings, 0:20:57.820,0:21:02.798 'hello ' and 'there'. And it says hmm, this must[br]be a concatenation. 0:21:02.798,0:21:07.040 Alright? So I'm going to concatenate those[br]together because 0:21:07.040,0:21:10.070 those are strings and I know how to[br]concatenate strings. 0:21:10.070,0:21:12.660 And that's kind of like string addition,[br]right? 0:21:14.060,0:21:18.460 And so we see a "hello there" as a result.[br]Now the interesting thing is, where 0:21:18.460,0:21:21.760 did this space come from?[br]Let me change colors here. 0:21:21.760,0:21:22.690 Oops. 0:21:22.690,0:21:27.050 Where did that space come from?[br]Well, the plus does not add the space. 0:21:27.050,0:21:29.750 Here's a space right there, and that's the[br]space. 0:21:29.750,0:21:34.930 So I can concatenate it, hello space plus[br]there, and that's how I got hello there. 0:21:34.930,0:21:36.690 But, the key thing is, is this plus 0:21:36.690,0:21:42.930 operator, clear, this plus operator looks[br]to either side 0:21:42.930,0:21:43.700 and says oh, 0:21:43.700,0:21:46.790 they're strings.[br]I think you mean concatenation. 0:21:46.790,0:21:49.290 Here it looks either side and says oh, 0:21:49.290,0:21:51.970 those are integers, I think you mean[br]addition. 0:21:51.970,0:21:57.610 So Python is very aware of type and type[br]informs Python what you really mean. 0:21:57.610,0:21:58.952 So, it looks like those are kind 0:21:58.952,0:22:01.270 of the same, but they're quite different[br]operations. 0:22:03.990,0:22:09.090 So the type can get you into trouble.[br]Remember Python is looking at the type. 0:22:09.090,0:22:10.840 So here we have a little problem, our 0:22:10.840,0:22:14.880 first traceback, first of many [br]tracebacks. 0:22:14.880,0:22:19.500 So here we have ee which is hello there[br]which is 0:22:19.500,0:22:22.330 exactly what we did. This is a string and[br]this is a string. 0:22:22.330,0:22:27.150 So ee should be a string. And then we try[br]to add 1 to it. 0:22:27.150,0:22:29.170 And again, Python is saying oh, I see 0:22:29.170,0:22:32.130 a plus sign here, so I'm going to look[br]over here, yeah, 0:22:32.130,0:22:34.240 that's a string, and look over here, and[br]that's an integer. 0:22:34.240,0:22:37.780 And it's like, aaah! And this is a traceback. 0:22:37.780,0:22:41.070 Now, here's a good time to talk about[br]tracebacks. 0:22:41.070,0:22:43.370 Tracebacks, I color them red. 0:22:43.370,0:22:47.480 Because you might think that Python[br]dislikes you or 0:22:47.480,0:22:50.780 thinks that you're, you know, unworthy of[br]its brilliance. 0:22:51.860,0:22:54.280 And certainly the way these things are[br]worded it sounds like, 0:22:54.280,0:22:58.190 you know, the, you're being scolded.[br]It's like, hey, type error. 0:22:58.190,0:23:01.800 You can, cannot concatenate str and int[br]objects, right? 0:23:01.800,0:23:05.800 That's, I'm, I'm scolding you, you bad,[br]bad programmer. 0:23:05.800,0:23:08.040 And it does feel a bit like you're[br]scolded. 0:23:08.040,0:23:11.740 But, if you go back to lecture one, this[br]is also 0:23:11.740,0:23:16.010 the moment where, really, we shouldn't[br]think of this as like scolding. 0:23:16.010,0:23:18.550 We should think of this as Python sort of[br]asking for help. 0:23:18.550,0:23:19.700 It's like, 0:23:19.700,0:23:25.510 wow, you gave me this line, and I, Python,[br]have no idea. 0:23:25.510,0:23:28.970 In all your greatness, could you give me[br]some possible 0:23:28.970,0:23:30.810 clue as to what you really [br]mean for me to do? 0:23:30.810,0:23:32.260 Because I'm so lost. 0:23:32.260,0:23:35.730 And given that I'm Python and I'm lost and[br]you are the only 0:23:35.730,0:23:41.420 purpose for my existence, I must stop[br]until you give me better guidance. 0:23:41.420,0:23:44.554 So, don't look at tracebacks as scolding. 0:23:44.554,0:23:50.305 They sound like scolding.[br]I'll stop coloring them red after a while. 0:23:50.305,0:23:54.360 So, if Python is so obsessed with the type[br]of things, you 0:23:54.360,0:23:57.030 should be able to ask Python what the type[br]of something is. 0:23:57.030,0:23:59.720 So there's a built-in function called[br]type. 0:23:59.720,0:24:01.410 This is part of the Python language. 0:24:01.410,0:24:04.750 Type (), and you can put a variable in[br]here. 0:24:04.750,0:24:06.450 What's the type of the variable ee? 0:24:06.450,0:24:10.180 And it says, oh yeah, I know what that is,[br]that would be a string. 0:24:10.180,0:24:12.060 And then you can also put a constant in[br]here. 0:24:12.060,0:24:15.640 And say what's the type of quote, hello,[br]quote, and that's a string too. 0:24:15.640,0:24:17.210 And what's the type of the number 1? 0:24:17.210,0:24:19.010 Well that would be an integer. 0:24:19.010,0:24:20.660 So it's picky about the type, but it will 0:24:20.660,0:24:23.630 also share with you what it believes the[br]type is. 0:24:24.990,0:24:28.120 And there's several types of numbers. 0:24:28.120,0:24:31.540 As I've already mentioned, there are[br]integers, which are the whole numbers. 0:24:31.540,0:24:33.960 They can be positive and negative and[br]zero. 0:24:33.960,0:24:35.310 And then there are the decimal numbers, 0:24:35.310,0:24:42.060 the floating point numbers,[br]like 98.6 or negative 2.5 or 14.0. 0:24:42.060,0:24:46.308 Python knows these as well because it does[br]division different if it's presented with 0:24:46.308,0:24:49.610 two integers, or an integer and a float,[br]or a float and a float. 0:24:54.100,0:24:57.620 And so here we have x is 1, and we'll say[br]what is it? 0:24:57.620,0:24:58.890 It's an integer. 0:24:58.890,0:25:01.600 And we say it's 98.6, and we'll say, well,[br]what's that? 0:25:01.600,0:25:02.810 It's a float. 0:25:02.810,0:25:05.130 And you can ask for both variables and[br]constants. 0:25:05.130,0:25:07.400 So what's the type of 1? It's an integer. 0:25:07.400,0:25:09.700 And what's type of up 1.0? [br]And it's a float. 0:25:11.550,0:25:12.850 You can also convert types. 0:25:12.850,0:25:16.370 It has a bunch of type conversion[br]functions built into it. 0:25:16.370,0:25:19.170 So, there's implicit conversion going on 0:25:19.170,0:25:23.110 when you're sort of saying, you know,[br]divide an integer by a floating point. 0:25:23.110,0:25:25.690 It says okay I see, I look to [br]the sides and 0:25:25.690,0:25:28.890 I will make the, I will make the[br]conversion for you. 0:25:28.890,0:25:30.400 But you can also be explicit. 0:25:30.400,0:25:32.760 So in this case we're going to say, [br]take this 0:25:32.760,0:25:35.848 99 and convert to a floating point[br]version of itself. 0:25:35.848,0:25:39.060 Which is 99.0.[br]And then do the division. 0:25:39.060,0:25:41.920 So Python looks out here and goes oh,[br]after that, that's 0:25:41.920,0:25:44.920 a float, and that's an integer if I look[br]over here. 0:25:44.920,0:25:47.540 And then that means that the [br]result is a float. 0:25:47.540,0:25:49.360 And the division is done as a float. 0:25:49.360,0:25:55.330 So we are force converting the 99 integer[br]into a 99.0 float. 0:25:57.180,0:25:59.460 And we can even do this like and just[br]stick it in the variable. 0:25:59.460,0:26:02.790 So we can just put 42 in i and that is an[br]integer. 0:26:02.790,0:26:06.530 Then we can say, hey, convert float that i 0:26:06.530,0:26:09.750 into a float and stick it into the[br]variable f. 0:26:09.750,0:26:13.600 And so we can print it.[br]And now it's 42.0 instead of 42. 0:26:13.600,0:26:16.426 Right? They're not the same. 0:26:16.426,0:26:18.180 They're both kind of 42, but one is a 0:26:18.180,0:26:20.900 floating point 42 and the other is an[br]integer 42. 0:26:20.900,0:26:24.110 And we can ask, and that is a float. 0:26:24.110,0:26:26.320 And you can also do the same thing in the[br]middle of 0:26:26.320,0:26:30.510 a calculation, where you have 1 plus 2[br]times a float of 3. 0:26:30.510,0:26:34.812 This float is done quickly.[br]So the first thing that happens 0:26:34.812,0:26:39.130 this is 1 plus 2 times 3.0 over 4[br]minus 5. 0:26:39.130,0:26:39.630 So 0:26:41.800,0:26:43.540 the first thing that happens is these[br]floats 0:26:43.540,0:26:45.740 are done because they are parentheses so[br]they matter. 0:26:45.740,0:26:49.640 So this is a built-in function called[br]float that takes, as its 0:26:49.640,0:26:55.390 argument, a non-floating point number and[br]gives you back a floating point number. 0:26:55.390,0:26:57.390 We'll talk more about functions [br]in Chapter Four. 0:27:01.230,0:27:05.640 You can also convert between strings and[br]numbers, and if you 0:27:05.640,0:27:10.220 recall, I, we did the example where we[br]took a string. 0:27:10.220,0:27:12.880 In this case, I'm being a little[br]confusing, because 0:27:12.880,0:27:15.680 I'm making a string with the [br]characters 1, 2, 3. 0:27:15.680,0:27:19.110 Now, this is not the same as 123. 0:27:19.110,0:27:23.530 This is a three-character string [br]with 1, 2, 3 in it. 0:27:23.530,0:27:26.460 And I can ask what kind of thing is in[br]there, and it says, 0:27:26.460,0:27:28.810 oh, there's a string in there.[br]I know about that. 0:27:28.810,0:27:30.490 And then I can try to add 1 to it, and 0:27:30.490,0:27:35.690 it seems intuitive that quote 123 plus 1[br]would be somehow 124. 0:27:35.690,0:27:37.966 But it's not. 0:27:37.966,0:27:40.474 Python takes a look at the plus and says,[br]oh there's 0:27:40.474,0:27:43.440 a string on that side, and an integer on[br]that side. 0:27:43.440,0:27:45.720 I am going to freak out and tell you 0:27:45.720,0:27:48.940 that you cannot concatenate [br]a string and an integer. 0:27:48.940,0:27:51.710 Okay?[br]But there is an int function 0:27:51.710,0:27:55.360 that converts various things, including[br]strings, to an integer. 0:27:55.360,0:28:00.520 So we can give as its parameter, its input,[br]the string value, then it 0:28:00.520,0:28:05.450 converts it to an integer, and then we'll[br]put the result in the variable ival. 0:28:05.450,0:28:09.840 We can ask what the type of that is, it's[br]an i, it's a integer. 0:28:09.840,0:28:13.360 And now we can use it in an expression,[br]print ival plus 1, and 0:28:13.360,0:28:17.230 so now Python looks to both sides, sees an[br]integer, sees an integer, and 0:28:17.230,0:28:19.530 gets 124.[br]Voila. 0:28:21.090,0:28:24.880 Now, if I make a new variable and I stick[br]hello Bob in it, and I 0:28:24.880,0:28:31.340 say hey, let's convert hello Bob to an[br]integer, as you might expect, it blows up. 0:28:31.340,0:28:33.850 And it says, invalid literal [br]for int. 0:28:35.980,0:28:42.310 These, these tracebacks again, once you[br]kind of get used to the kind of harsh 0:28:42.310,0:28:45.242 wording of them, because they're not[br]saying, sorry, comma, 0:28:45.242,0:28:48.010 they're trying to tell you what's[br]going on. 0:28:48.010,0:28:52.930 So, cannot concatenate string and integer,[br]and invalid literal for int. 0:28:52.930,0:28:55.140 It's trying to be as helpful as it[br]possibly can 0:28:55.140,0:28:57.710 be to give you a clue as to what to fix. 0:28:57.710,0:29:00.270 So, again, not scolded. 0:29:02.030,0:29:05.610 Okay, so that's variables and types and[br]type conversion. 0:29:05.610,0:29:09.563 Now we'll talk a little bit about [br]user input. 0:29:09.563,0:29:15.360 And there's a function that's built into[br]Python called raw_input. 0:29:15.360,0:29:21.080 And what happens when raw_input runs is[br]it, it has as one of 0:29:21.080,0:29:25.310 its parameters, a prompt, which is[br]something that shows up on the screen. 0:29:25.310,0:29:26.020 Who are you? 0:29:26.020,0:29:27.820 And then, 0:29:27.820,0:29:34.520 it waits, tik, tik, tik, tik, tik.[br]Sits and waits, says, what next? 0:29:34.520,0:29:36.920 And then, you type a string, [br]dot, dot, dot, dot, dot, 0:29:36.920,0:29:38.740 and then you hit the Enter key. 0:29:40.540,0:29:41.640 The Enter key. 0:29:41.640,0:29:47.310 And then whatever you typed here goes[br]into a variable. 0:29:48.510,0:29:52.650 And it is a string.[br]And, then you, 0:29:52.650,0:29:53.656 then you can use it. 0:29:53.656,0:29:55.670 So I'm going to print the string Welcome, 0:29:55.670,0:29:58.710 comma. So that means I'm printing two[br]things now. 0:29:58.710,0:30:01.740 The comma adds a space between Welcome and[br]then nam, and so 0:30:01.740,0:30:07.160 Welcome is a literal, and then Chuck is[br]coming from this nam variable. 0:30:07.160,0:30:09.380 So this is a two-line program. 0:30:09.380,0:30:12.430 I think this is one of your [br]assignments, actually, 0:30:12.430,0:30:15.930 to well, it's one of the exercises [br]in the book. 0:30:15.930,0:30:17.840 To a prompt for a user's name, and 0:30:17.840,0:30:19.550 then welcome them, okay? 0:30:21.382,0:30:26.380 So raw_input is a function that issues a[br]prompt, waits, and then takes whatever 0:30:26.380,0:30:29.670 string that's entered, and then returns it[br]and then puts it into that variable. 0:30:33.390,0:30:38.190 So, now we're going to create kind of the[br]first useful program. 0:30:38.190,0:30:41.160 It's not a powerful program. 0:30:41.160,0:30:48.630 It is a, an interesting problem of the[br]fact that for some reason there 0:30:48.630,0:30:50.190 is a difference in the numbering scheme 0:30:50.190,0:30:53.140 of United States elevators and [br]European elevators. 0:30:54.180,0:30:58.440 European elevators, the floor that you[br]walk out on is the 0:30:58.440,0:30:59.750 zero floor. 0:30:59.750,0:31:01.700 The floor above that is the [br]one floor and the 0:31:01.700,0:31:06.300 floor below that, the basement, [br]is the minus one floor. 0:31:06.300,0:31:11.198 And so you walk in and you can go either[br]up the elevator or down the elevator. 0:31:11.198,0:31:15.240 Of course, in the United States, the floor[br]that you walk in is the 0:31:15.240,0:31:20.120 one and then there's the two floor above[br]that and then there's like, the basement. 0:31:20.120,0:31:23.460 So this is the[br]imagination that the Americans 0:31:23.460,0:31:26.230 have as to how to number floors, right? 0:31:26.230,0:31:29.290 The Europeans go zero, one, minus one. 0:31:29.290,0:31:35.100 So, children who go to hotels learn[br]instantly the notion of zero and the 0:31:35.100,0:31:36.790 notion of positive and negative [br]numbers and 0:31:36.790,0:31:39.280 the symmetry between positive and [br]negative numbers. 0:31:39.280,0:31:44.520 I mean, I just wish the United States[br]hotels would switch to this 0:31:44.520,0:31:49.000 to teach young people zero immediately and 0:31:49.000,0:31:50.190 negative numbers. 0:31:50.190,0:31:53.730 So we somehow think that numbers all in[br]the United States start at 1 0:31:53.730,0:31:57.030 and then there are no[br]negative numbers, there's the 0:31:57.030,0:31:57.530 basement. 0:32:00.100,0:32:01.990 I wonder why that is, but whatever. 0:32:03.630,0:32:07.150 For people who travel a lot, they may be[br]confused by this. 0:32:08.190,0:32:09.780 They need a way to convert back and 0:32:09.780,0:32:14.009 forth between the US and European[br]numbering system. 0:32:15.500,0:32:18.170 So this is a simple program that[br]demonstrates 0:32:18.170,0:32:22.160 a real classic pattern of input processing[br]and output. 0:32:22.160,0:32:25.380 It's just three lines, but it has the 0:32:25.380,0:32:28.940 essential things that all programs that[br]are useful. 0:32:28.940,0:32:32.600 They generally read some data, [br]do some work with 0:32:32.600,0:32:36.070 the data, and then produce some [br]kind of results. 0:32:36.070,0:32:40.740 And so, so the first line is a raw_input 0:32:40.740,0:32:45.240 that effectively, that puts out a prompt[br]and then it waits. 0:32:45.240,0:32:49.190 It says, please enter your Europe floor.[br]It sits there. 0:32:49.190,0:32:50.920 We type a zero, 0:32:50.920,0:32:54.100 then zero goes into inp, but it is a[br]string. 0:32:55.250,0:32:56.303 It's not a number. 0:32:56.303,0:32:57.700 It's a string. 0:32:57.700,0:33:00.870 So we can't add to it. But we can take 0:33:02.100,0:33:04.800 and convert it to an integer with [br]the int function. 0:33:04.800,0:33:07.710 Int of inp, thats a string being [br]converted to an integer 0:33:07.710,0:33:10.370 so now its a real numeric zero. 0:33:10.370,0:33:14.200 And we can add 1 to that and we [br]sum that together. 0:33:14.200,0:33:16.210 And we put it into the 0:33:16.210,0:33:20.800 variable usf and then we print US floor,[br]comma, and then 0:33:20.800,0:33:25.350 whatever the variable for usf is. And out[br]comes US floor 1. 0:33:25.350,0:33:29.280 So we've written a very simple elevator[br]floor conversion 0:33:29.280,0:33:32.100 from a European floor to a [br]United States floor. 0:33:33.310,0:33:35.810 Don't ask about negative numbers, it's not[br]really good at that. 0:33:35.810,0:33:39.120 But from zero and positive numbers it[br]works great. 0:33:42.710,0:33:48.130 So another thing to think about in any[br]programming language is comments. 0:33:49.580,0:33:54.410 Comments are like commentary, you know,[br]and basically it's a way for us to 0:33:55.880,0:34:00.410 add notations for ourselves and for other[br]humans interspersed in the code. 0:34:01.480,0:34:06.680 And so in Python anything after a pound[br]sign is ignored. 0:34:06.680,0:34:07.900 You can have a pound sign at the beginning 0:34:07.900,0:34:09.545 of a line and then the [br]whole line is ignored. 0:34:09.545,0:34:12.670 There are two or three reasons why you[br]could do this. 0:34:12.670,0:34:15.699 One is sort of like paragraph headings,[br]where you can 0:34:15.699,0:34:21.190 say what's going to happen in English or,[br]or your language. 0:34:21.190,0:34:23.010 And you can write documentation says this 0:34:23.010,0:34:27.510 code was written by Charles Severence,[br]December 2010. 0:34:27.510,0:34:29.800 And you can also just hide [br]a line of code to 0:34:29.800,0:34:32.920 test and, and turn it on and off without[br]actually deleting 0:34:32.920,0:34:36.900 the line of code.[br]It's a real common thing in debugging. 0:34:36.900,0:34:43.070 So for example, here is a, here is a, the[br]program that we've been playing with. 0:34:43.070,0:34:45.159 This is our word counting program that 0:34:45.159,0:34:46.960 we've been talking about [br]from the beginning. 0:34:46.960,0:34:51.850 And here is an example of four comments,[br]one, two, three, four. 0:34:51.850,0:34:56.330 Four comments that basically tell us what[br]these paragraphs are going to do. 0:34:56.330,0:34:59.250 Now, they don't have any effect on the[br]program whatsoever. 0:34:59.250,0:35:01.150 But this one says get the name of the file[br]and open it. 0:35:02.290,0:35:03.860 Kind of helpful, right? 0:35:03.860,0:35:04.910 Count the word frequency. 0:35:04.910,0:35:07.870 That's what this little bit does.[br]Find the most common word. 0:35:07.870,0:35:09.490 That's what this little bit does. 0:35:09.490,0:35:11.500 And all done, print this out. 0:35:11.500,0:35:16.450 So it's really can be very helpful just to[br]add a little tiny bit of stuff. 0:35:16.450,0:35:18.410 And you don't want to overuse comments. 0:35:18.410,0:35:23.470 You don't want to say like x equals 12,[br]take 12 and put it into x. 0:35:23.470,0:35:24.530 Sometimes people teach 0:35:24.530,0:35:28.360 you and try to say, oh you need a one[br]comment every three lines. 0:35:28.360,0:35:29.760 I don't believe in any of those rules. 0:35:29.760,0:35:33.230 I basically say if its useful to describe[br]it, then describe it. 0:35:34.920,0:35:41.100 So that's comments.[br]So some operators apply to strings. 0:35:41.100,0:35:43.410 We've already talked about plus. 0:35:43.410,0:35:45.600 It's kind of silly, although [br]useful in places. 0:35:45.600,0:35:49.660 You can actually multiply strings.[br]The asterisk looks and 0:35:49.660,0:35:54.170 says I've got a string and an integer, and[br]it prints out the string five times. 0:35:55.300,0:35:56.120 Not a lot of use for that. 0:35:57.620,0:36:01.420 Now, let's talk a little bit about[br]choosing variable names. 0:36:01.420,0:36:04.280 This is something that is [br]really confusing. 0:36:04.280,0:36:08.170 So I said like, x equals 1, x equals x[br]plus 1, what does x mean? 0:36:08.170,0:36:12.190 And the answer is, it doesn't [br]mean anything. 0:36:12.190,0:36:14.970 I chose it.[br]I wanted to make a variable, 0:36:14.970,0:36:16.770 and so I picked x. 0:36:16.770,0:36:19.100 We pick x a lot, probably because [br]we learned 0:36:19.100,0:36:23.210 in algebra in sixth grade that [br]x was a variable. 0:36:23.210,0:36:26.270 So, and it's short, and so, [br]why not call it x? 0:36:29.400,0:36:32.610 But as your programs get larger this gets[br]kind of frustrating 0:36:32.610,0:36:35.510 to have all your variables like x [br]and y and z. 0:36:35.510,0:36:39.120 And so the notion of mnemonic, it means[br]memory aid. 0:36:39.120,0:36:44.150 We choose our variable names wisely, so[br]they remind us of what the variable's 0:36:44.150,0:36:49.360 going to do internally.[br]And so, as I go through this lecture 0:36:51.540,0:36:55.310 in the beginning if I choose a variable[br]that's too clever 0:36:55.310,0:36:59.050 you're going to think that it's[br]part of the language. 0:36:59.050,0:37:02.560 And so I sort of switch back and forth[br]between well-chosen variable names 0:37:02.560,0:37:07.055 and stupid variable names to kind of[br]reemphasize the notion that I can choose. 0:37:07.055,0:37:10.140 Mnemonic is a good practice, okay? 0:37:10.140,0:37:13.860 So here we go.[br]Let's take a look at a bit of code. 0:37:17.210,0:37:21.100 So the question is, what is [br]this code doing? 0:37:21.100,0:37:23.290 What will it even print out? 0:37:23.290,0:37:24.890 Is it syntactically correct? 0:37:27.380,0:37:33.330 Now you could probably cut and paste this[br]into your brow, into Python and figure 0:37:33.330,0:37:39.266 out that it is syntactically correct.[br]There are three variables. 0:37:41.266,0:37:46.670 This one here and this one here match. 0:37:47.800,0:37:52.860 This one here and that one there match.[br]And these two match. 0:37:53.860,0:37:55.360 So it's taking these two numbers and 0:37:55.360,0:37:57.890 multiplying together, and then printing[br]out the product 0:37:57.890,0:38:03.790 of the two numbers, if you're real careful[br]and like look at every, every character. 0:38:03.790,0:38:07.180 Now, this would be called [br]non-mnemonic variables. 0:38:07.180,0:38:09.200 They're really messy. 0:38:09.200,0:38:13.290 Now Python, it's happy, because all it[br]wants is to say, oh. 0:38:13.290,0:38:13.800 Here is then name that 0:38:13.800,0:38:16.550 I, the programmer, decided I[br]wanted to call this 0:38:16.550,0:38:20.470 piece of memory and I'll refer to [br]it down here, okay? 0:38:20.470,0:38:23.320 And so Python is happy. 0:38:23.320,0:38:27.280 Now if you hand this to another human[br]being they are going to be really unhappy. 0:38:27.280,0:38:29.170 Because they are going to be like, what[br]are you doing? 0:38:30.270,0:38:35.660 So one better way to write it would be to[br]make the variables very simple. 0:38:35.660,0:38:39.134 And then cognitively we humans can figure[br]out which is which, 0:38:39.134,0:38:42.720 because again it's still only [br]about matching. 0:38:42.720,0:38:48.470 The a has to match the a, the b matches[br]the b, and the c's match. 0:38:48.470,0:38:50.290 It's actually the exact same program. 0:38:50.290,0:38:53.570 A equals 35.[br]B equals 12.5. 0:38:53.570,0:38:54.820 C equals A times B. 0:38:54.820,0:38:57.740 And print C.[br]It is these. 0:38:57.740,0:39:00.980 Python sees these as the same program. 0:39:00.980,0:39:04.210 It doesn't care what we name them.[br]Now, a human will 0:39:04.210,0:39:08.770 be much appreciative if you say, here you[br]can either have this one or this one. 0:39:08.770,0:39:11.070 This one will make them a lot happier. 0:39:13.450,0:39:14.480 Okay? 0:39:14.480,0:39:18.610 So that is certainly cognitively easier,[br]but it's not really 0:39:18.610,0:39:23.240 giving you any sense of what's going on[br]here, right? 0:39:23.240,0:39:29.260 So an even better way to write this exact[br]same program to do the exact same thing 0:39:29.260,0:39:32.430 would be to choose variables [br]named hours, rate, and pay, 0:39:32.430,0:39:35.810 if indeed that is what you're doing. 0:39:35.810,0:39:38.610 Now you can look at this and you go, [br]oh well, shoot, 0:39:38.610,0:39:42.730 35 is the number of hours, and 12.5 is the[br]rate, and the pay is 0:39:42.730,0:39:46.090 the number of hours times the rate, and[br]then we are going to print out the pay. 0:39:46.090,0:39:48.060 And that makes a lot of sense. 0:39:48.060,0:39:54.290 So this is really a awesome and wonderful[br]characterization. 0:39:54.290,0:39:56.592 And if that's what you're doing [br]and those are hours, 0:39:56.592,0:40:00.090 rate, and pay, it's a great thing [br]to name the variables. 0:40:00.090,0:40:04.370 But, this is where beginning students [br]get confused. 0:40:04.370,0:40:07.415 And so sometimes I'll write it this way[br]and sometimes I'll write it this way. 0:40:07.415,0:40:09.749 Because you'll look at this, until you get[br]a little 0:40:09.749,0:40:12.880 more sophisticated, a little more skilled,[br]and you'll say like 0:40:15.180,0:40:19.430 does Python know something about payroll?[br]Is hours a reserved word? 0:40:19.430,0:40:22.370 Is rate a reserved word and pay [br]a reserved word? 0:40:22.370,0:40:26.350 Are these things that Python knows about?[br]And the answer is, no. 0:40:26.350,0:40:30.270 Python sees these three programs as[br]exactly the same name. 0:40:30.270,0:40:34.440 It's just this person really made a very[br]bad choice of variable name. 0:40:34.440,0:40:37.740 This person made a less bad choice of[br]variable name, 0:40:37.740,0:40:40.830 and this person made a really awesome[br]choice of variable name. 0:40:40.830,0:40:43.010 So the only difference between those two[br]things is style. 0:40:45.100,0:40:47.260 They are the exact same program. 0:40:47.260,0:40:51.460 And Python is equivalently happy with[br]these, but humans 0:40:51.460,0:40:55.280 are most happy when the variables are [br]easy to remember 0:40:55.280,0:40:58.840 and they are somewhat descriptive of what[br]their expected contents will be. 0:40:59.840,0:41:01.530 That's mnemonic. 0:41:01.530,0:41:05.680 To help you remember what you were meaning[br]to do when you write the program. 0:41:05.680,0:41:08.280 This is still a bit cryptic, having these 0:41:08.280,0:41:10.340 really short, one-character variable[br]names is still 0:41:10.340,0:41:12.320 a bit cryptic.[br]So, 0:41:14.460,0:41:17.220 You have a couple of assignments at the[br]end of the chapter. 0:41:17.220,0:41:20.740 One of the assignments is to write a[br]program to prompt 0:41:20.740,0:41:25.220 the user for hours and rate per hour and[br]compute pay. 0:41:26.430,0:41:32.566 So, I won't do this here, but just a[br]couple of sort of odd things. 0:41:32.566,0:41:34.755 You're going to be using raw_input. 0:41:34.755,0:41:39.528 But remember that hands a string in so[br]you're going 0:41:39.528,0:41:41.980 to have to use float. 0:41:44.030,0:41:45.690 The function to convert it to a floating 0:41:45.690,0:41:47.755 point number so you can actually do a[br]calculation. 0:41:47.755,0:41:51.170 And then you're going to have to use[br]multiplication and print. 0:41:51.170,0:41:53.210 So then multiplication, and then print. 0:41:55.200,0:41:59.540 So some combination of raw input, float,[br]multiplication, and print, 0:42:00.790,0:42:04.170 constructed to make your program do[br]exactly this. 0:42:05.690,0:42:08.350 So, this is the end of Chapter Two. 0:42:08.350,0:42:09.210 We talked about types, 0:42:09.210,0:42:14.400 reserved words, variables, the[br]mnemonic, how you choose variable names. 0:42:14.400,0:42:15.835 We'll hit this a couple more times 0:42:15.835,0:42:18.400 because choosing variable names is always[br]problematic. 0:42:18.400,0:42:22.400 Operators, operator precedence, which just[br]means like does multiplication happen 0:42:22.400,0:42:26.980 before plus, parentheses.[br]Integer division is a little weird because 0:42:26.980,0:42:34.255 it truncates, oops, right, 9 over 10. 0:42:34.255,0:42:41.050 Oops, 9 over 10 equals 0.[br]That's the integer division truncating. 0:42:41.050,0:42:47.930 Conversion, this is like the int()[br]float(). 0:42:47.930,0:42:50.030 And then user input, which is raw_input. 0:42:50.030,0:42:52.340 And then comments, which are ways for you 0:42:52.340,0:42:55.690 to add human-readable text to your[br]program. 0:42:55.690,0:42:57.560 Okay? See you next lecture.