1 00:00:00,800 --> 00:00:05,540 So, here we are, talking about integers and floating points. 2 00:00:05,540 --> 00:00:09,520 These are a concept in programming languages and in Python called type. 3 00:00:10,820 --> 00:00:13,270 Variables and constants have a type. 4 00:00:14,600 --> 00:00:17,540 We can see that if you say 1, versus 1.0, 5 00:00:17,540 --> 00:00:21,090 they have different, they, it works different, it functions differently. 6 00:00:21,090 --> 00:00:27,300 And so Python keeps track of both variables and literals/constants, and 7 00:00:27,300 --> 00:00:31,240 having them have a type. And we've seen this, right? 8 00:00:31,240 --> 00:00:33,920 Now, the interesting thing is, is Python is very aware of 9 00:00:33,920 --> 00:00:38,920 the type and can use the same syntax to accomplish different things. 10 00:00:38,920 --> 00:00:43,200 So if we look at this line here, where we say dd equals 1.4. 11 00:00:43,200 --> 00:00:44,790 Well it looks at the 1 and looks at the 4 and it says, 12 00:00:44,790 --> 00:00:47,770 oh those are two integers. I will add those together and give you a 5. 13 00:00:47,770 --> 00:00:52,210 So it gives you an integer, an integer, and an integer comes back, 14 00:00:52,210 --> 00:00:52,310 Okay? 15 00:00:52,310 --> 00:00:56,820 And then ee equals 'hello ' plus 'there'. Well these are two strings, 16 00:00:56,820 --> 00:01:01,798 'hello ' and 'there'. And it says hmm, this must be a concatenation. 17 00:01:01,798 --> 00:01:06,040 Alright? So I'm going to concatenate those together because 18 00:01:06,040 --> 00:01:09,070 those are strings and I know how to concatenate strings. 19 00:01:09,070 --> 00:01:11,660 And that's kind of like string addition, right? 20 00:01:13,060 --> 00:01:17,460 And so we see a "hello there" as a result. Now the interesting thing is, where 21 00:01:17,460 --> 00:01:20,760 did this space come from? Let me change colors here. 22 00:01:20,760 --> 00:01:21,690 Oops. 23 00:01:21,690 --> 00:01:26,050 Where did that space come from? Well, the plus does not add the space. 24 00:01:26,050 --> 00:01:28,750 Here's a space right there, and that's the space. 25 00:01:28,750 --> 00:01:33,930 So I can concatenate it, hello space plus there, and that's how I got hello there. 26 00:01:33,930 --> 00:01:35,690 But, the key thing is, is this plus 27 00:01:35,690 --> 00:01:41,930 operator, clear, this plus operator looks to either side 28 00:01:41,930 --> 00:01:42,700 and says oh, 29 00:01:42,700 --> 00:01:45,790 they're strings. I think you mean concatenation. 30 00:01:45,790 --> 00:01:48,290 Here it looks either side and says oh, 31 00:01:48,290 --> 00:01:50,970 those are integers, I think you mean addition. 32 00:01:50,970 --> 00:01:56,610 So Python is very aware of type and type informs Python what you really mean. 33 00:01:56,610 --> 00:01:57,952 So, it looks like those are kind 34 00:01:57,952 --> 00:02:00,270 of the same, but they're quite different operations. 35 00:02:02,990 --> 00:02:08,090 So the type can get you into trouble. Remember Python is looking at the type. 36 00:02:08,090 --> 00:02:09,840 So here we have a little problem, our 37 00:02:09,840 --> 00:02:13,880 first traceback, first of many tracebacks. 38 00:02:13,880 --> 00:02:18,500 So here we have ee which is hello there which is 39 00:02:18,500 --> 00:02:21,330 exactly what we did. This is a string and this is a string. 40 00:02:21,330 --> 00:02:26,150 So ee should be a string. And then we try to add 1 to it. 41 00:02:26,150 --> 00:02:28,170 And again, Python is saying oh, I see 42 00:02:28,170 --> 00:02:31,130 a plus sign here, so I'm going to look over here, yeah, 43 00:02:31,130 --> 00:02:33,240 that's a string, and look over here, and that's an integer. 44 00:02:33,240 --> 00:02:36,780 And it's like, aaah! And this is a traceback. 45 00:02:36,780 --> 00:02:40,070 Now, here's a good time to talk about tracebacks. 46 00:02:40,070 --> 00:02:42,370 Tracebacks, I color them red. 47 00:02:42,370 --> 00:02:46,480 Because you might think that Python dislikes you or 48 00:02:46,480 --> 00:02:49,780 thinks that you're, you know, unworthy of its brilliance. 49 00:02:50,860 --> 00:02:53,280 And certainly the way these things are worded it sounds like, 50 00:02:53,280 --> 00:02:57,190 you know, the, you're being scolded. It's like, hey, type error. 51 00:02:57,190 --> 00:03:00,800 You can, cannot concatenate str and int objects, right? 52 00:03:00,800 --> 00:03:04,800 That's, I'm, I'm scolding you, you bad, bad programmer. 53 00:03:04,800 --> 00:03:07,040 And it does feel a bit like you're scolded. 54 00:03:07,040 --> 00:03:10,740 But, if you go back to lecture one, this is also 55 00:03:10,740 --> 00:03:15,010 the moment where, really, we shouldn't think of this as like scolding. 56 00:03:15,010 --> 00:03:17,550 We should think of this as Python sort of asking for help. 57 00:03:17,550 --> 00:03:18,700 It's like, 58 00:03:18,700 --> 00:03:24,510 wow, you gave me this line, and I, Python, have no idea. 59 00:03:24,510 --> 00:03:27,970 In all your greatness, could you give me some possible 60 00:03:27,970 --> 00:03:29,810 clue as to what you really mean for me to do? 61 00:03:29,810 --> 00:03:31,260 Because I'm so lost. 62 00:03:31,260 --> 00:03:34,730 And given that I'm Python and I'm lost and you are the only 63 00:03:34,730 --> 00:03:40,420 purpose for my existence, I must stop until you give me better guidance. 64 00:03:40,420 --> 00:03:43,554 So, don't look at tracebacks as scolding. 65 00:03:43,554 --> 00:03:49,305 They sound like scolding. I'll stop coloring them red after a while. 66 00:03:49,305 --> 00:03:53,360 So, if Python is so obsessed with the type of things, you 67 00:03:53,360 --> 00:03:56,030 should be able to ask Python what the type of something is. 68 00:03:56,030 --> 00:03:58,720 So there's a built-in function called type. 69 00:03:58,720 --> 00:04:00,410 This is part of the Python language. 70 00:04:00,410 --> 00:04:03,750 Type (), and you can put a variable in here. 71 00:04:03,750 --> 00:04:05,450 What's the type of the variable ee? 72 00:04:05,450 --> 00:04:09,180 And it says, oh yeah, I know what that is, that would be a string. 73 00:04:09,180 --> 00:04:11,060 And then you can also put a constant in here. 74 00:04:11,060 --> 00:04:14,640 And say what's the type of quote, hello, quote, and that's a string too. 75 00:04:14,640 --> 00:04:16,210 And what's the type of the number 1? 76 00:04:16,210 --> 00:04:18,010 Well that would be an integer. 77 00:04:18,010 --> 00:04:19,660 So it's picky about the type, but it will 78 00:04:19,660 --> 00:04:22,630 also share with you what it believes the type is. 79 00:04:23,990 --> 00:04:27,120 And there's several types of numbers. 80 00:04:27,120 --> 00:04:30,540 As I've already mentioned, there are integers, which are the whole numbers. 81 00:04:30,540 --> 00:04:32,960 They can be positive and negative and zero. 82 00:04:32,960 --> 00:04:34,310 And then there are the decimal numbers, 83 00:04:34,310 --> 00:04:41,060 the floating point numbers, like 98.6 or negative 2.5 or 14.0. 84 00:04:41,060 --> 00:04:45,308 Python knows these as well because it does division different if it's presented with 85 00:04:45,308 --> 00:04:48,610 two integers, or an integer and a float, or a float and a float. 86 00:04:53,100 --> 00:04:56,620 And so here we have x is 1, and we'll say what is it? 87 00:04:56,620 --> 00:04:57,890 It's an integer. 88 00:04:57,890 --> 00:05:00,600 And we say it's 98.6, and we'll say, well, what's that? 89 00:05:00,600 --> 00:05:01,810 It's a float. 90 00:05:01,810 --> 00:05:04,130 And you can ask for both variables and constants. 91 00:05:04,130 --> 00:05:06,400 So what's the type of 1? It's an integer. 92 00:05:06,400 --> 00:05:08,700 And what's type of up 1.0? And it's a float. 93 00:05:10,550 --> 00:05:11,850 You can also convert types. 94 00:05:11,850 --> 00:05:15,370 It has a bunch of type conversion functions built into it. 95 00:05:15,370 --> 00:05:18,170 So, there's implicit conversion going on 96 00:05:18,170 --> 00:05:22,110 when you're sort of saying, you know, divide an integer by a floating point. 97 00:05:22,110 --> 00:05:24,690 It says okay I see, I look to the sides and 98 00:05:24,690 --> 00:05:27,890 I will make the, I will make the conversion for you. 99 00:05:27,890 --> 00:05:29,400 But you can also be explicit. 100 00:05:29,400 --> 00:05:31,760 So in this case we're going to say, take this 101 00:05:31,760 --> 00:05:34,848 99 and convert to a floating point version of itself. 102 00:05:34,848 --> 00:05:38,060 Which is 99.0. And then do the division. 103 00:05:38,060 --> 00:05:40,920 So Python looks out here and goes oh, after that, that's 104 00:05:40,920 --> 00:05:43,920 a float, and that's an integer if I look over here. 105 00:05:43,920 --> 00:05:46,540 And then that means that the result is a float. 106 00:05:46,540 --> 00:05:48,360 And the division is done as a float. 107 00:05:48,360 --> 00:05:54,330 So we are force converting the 99 integer into a 99.0 float. 108 00:05:56,180 --> 00:05:58,460 And we can even do this like and just stick it in the variable. 109 00:05:58,460 --> 00:06:01,790 So we can just put 42 in i and that is an integer. 110 00:06:01,790 --> 00:06:05,530 Then we can say, hey, convert float that i 111 00:06:05,530 --> 00:06:08,750 into a float and stick it into the variable f. 112 00:06:08,750 --> 00:06:12,600 And so we can print it. And now it's 42.0 instead of 42. 113 00:06:12,600 --> 00:06:15,426 Right? They're not the same. 114 00:06:15,426 --> 00:06:17,180 They're both kind of 42, but one is a 115 00:06:17,180 --> 00:06:19,900 floating point 42 and the other is an integer 42. 116 00:06:19,900 --> 00:06:23,110 And we can ask, and that is a float. 117 00:06:23,110 --> 00:06:25,320 And you can also do the same thing in the middle of 118 00:06:25,320 --> 00:06:29,510 a calculation, where you have 1 plus 2 times a float of 3. 119 00:06:29,510 --> 00:06:33,812 This float is done quickly. So the first thing that happens 120 00:06:33,812 --> 00:06:38,130 this is 1 plus 2 times 3.0 over 4 minus 5. 121 00:06:38,130 --> 00:06:38,630 So 122 00:06:40,800 --> 00:06:42,540 the first thing that happens is these floats 123 00:06:42,540 --> 00:06:44,740 are done because they are parentheses so they matter. 124 00:06:44,740 --> 00:06:48,640 So this is a built-in function called float that takes, as its 125 00:06:48,640 --> 00:06:54,390 argument, a non-floating point number and gives you back a floating point number. 126 00:06:54,390 --> 00:06:56,390 We'll talk more about functions in Chapter Four. 127 00:07:00,230 --> 00:07:04,640 You can also convert between strings and numbers, and if you 128 00:07:04,640 --> 00:07:09,220 recall, I, we did the example where we took a string. 129 00:07:09,220 --> 00:07:11,880 In this case, I'm being a little confusing, because 130 00:07:11,880 --> 00:07:14,680 I'm making a string with the characters 1, 2, 3. 131 00:07:14,680 --> 00:07:18,110 Now, this is not the same as 123. 132 00:07:18,110 --> 00:07:22,530 This is a three-character string with 1, 2, 3 in it. 133 00:07:22,530 --> 00:07:25,460 And I can ask what kind of thing is in there, and it says, 134 00:07:25,460 --> 00:07:27,810 oh, there's a string in there. I know about that. 135 00:07:27,810 --> 00:07:29,490 And then I can try to add 1 to it, and 136 00:07:29,490 --> 00:07:34,690 it seems intuitive that quote 123 plus 1 would be somehow 124. 137 00:07:34,690 --> 00:07:36,966 But it's not. 138 00:07:36,966 --> 00:07:39,474 Python takes a look at the plus and says, oh there's 139 00:07:39,474 --> 00:07:42,440 a string on that side, and an integer on that side. 140 00:07:42,440 --> 00:07:44,720 I am going to freak out and tell you 141 00:07:44,720 --> 00:07:47,940 that you cannot concatenate a string and an integer. 142 00:07:47,940 --> 00:07:50,710 Okay? But there is an int function 143 00:07:50,710 --> 00:07:54,360 that converts various things, including strings, to an integer. 144 00:07:54,360 --> 00:07:59,520 So we can give as its parameter, its input, the string value, then it 145 00:07:59,520 --> 00:08:04,450 converts it to an integer, and then we'll put the result in the variable ival. 146 00:08:04,450 --> 00:08:08,840 We can ask what the type of that is, it's an i, it's a integer. 147 00:08:08,840 --> 00:08:12,360 And now we can use it in an expression, print ival plus 1, and 148 00:08:12,360 --> 00:08:16,230 so now Python looks to both sides, sees an integer, sees an integer, and 149 00:08:16,230 --> 00:08:18,530 gets 124. Voila. 150 00:08:20,090 --> 00:08:23,880 Now, if I make a new variable and I stick hello Bob in it, and I 151 00:08:23,880 --> 00:08:30,340 say hey, let's convert hello Bob to an integer, as you might expect, it blows up. 152 00:08:30,340 --> 00:08:32,850 And it says, invalid literal for int. 153 00:08:34,980 --> 00:08:41,310 These, these tracebacks again, once you kind of get used to the kind of harsh 154 00:08:41,310 --> 00:08:44,242 wording of them, because they're not saying, sorry, comma, 155 00:08:44,242 --> 00:08:47,010 they're trying to tell you what's going on. 156 00:08:47,010 --> 00:08:51,930 So, cannot concatenate string and integer, and invalid literal for int. 157 00:08:51,930 --> 00:08:54,140 It's trying to be as helpful as it possibly can 158 00:08:54,140 --> 00:08:56,710 be to give you a clue as to what to fix. 159 00:08:56,710 --> 00:08:59,270 So, again, not scolded. 160 00:09:01,030 --> 00:09:04,610 Okay, so that's variables and types and type conversion. 161 00:09:04,610 --> 00:09:08,563 Now we'll talk a little bit about user input. 162 00:09:08,563 --> 00:09:14,360 And there's a function that's built into Python called raw_input. 163 00:09:14,360 --> 00:09:20,080 And what happens when raw_input runs is it, it has as one of 164 00:09:20,080 --> 00:09:24,310 its parameters, a prompt, which is something that shows up on the screen. 165 00:09:24,310 --> 00:09:25,020 Who are you? 166 00:09:25,020 --> 00:09:26,820 And then, 167 00:09:26,820 --> 00:09:33,520 it waits, tik, tik, tik, tik, tik. Sits and waits, says, what next? 168 00:09:33,520 --> 00:09:35,920 And then, you type a string, dot, dot, dot, dot, dot, 169 00:09:35,920 --> 00:09:37,740 and then you hit the Enter key. 170 00:09:39,540 --> 00:09:40,640 The Enter key. 171 00:09:40,640 --> 00:09:46,310 And then whatever you typed here goes into a variable. 172 00:09:47,510 --> 00:09:51,650 And it is a string. And, then you, 173 00:09:51,650 --> 00:09:52,656 then you can use it. 174 00:09:52,656 --> 00:09:54,670 So I'm going to print the string Welcome, 175 00:09:54,670 --> 00:09:57,710 comma. So that means I'm printing two things now. 176 00:09:57,710 --> 00:10:00,740 The comma adds a space between Welcome and then nam, and so 177 00:10:00,740 --> 00:10:06,160 Welcome is a literal, and then Chuck is coming from this nam variable. 178 00:10:06,160 --> 00:10:08,380 So this is a two-line program. 179 00:10:08,380 --> 00:10:11,430 I think this is one of your assignments, actually, 180 00:10:11,430 --> 00:10:14,930 to well, it's one of the exercises in the book. 181 00:10:14,930 --> 00:10:16,840 To a prompt for a user's name, and 182 00:10:16,840 --> 00:10:18,550 then welcome them, okay? 183 00:10:20,382 --> 00:10:25,380 So raw_input is a function that issues a prompt, waits, and then takes whatever 184 00:10:25,380 --> 00:10:28,670 string that's entered, and then returns it and then puts it into that variable. 185 00:10:32,390 --> 00:10:37,190 So, now we're going to create kind of the first useful program. 186 00:10:37,190 --> 00:10:40,160 It's not a powerful program. 187 00:10:40,160 --> 00:10:47,630 It is a, an interesting problem of the fact that for some reason there 188 00:10:47,630 --> 00:10:49,190 is a difference in the numbering scheme 189 00:10:49,190 --> 00:10:52,140 of United States elevators and European elevators. 190 00:10:53,180 --> 00:10:57,440 European elevators, the floor that you walk out on is the 191 00:10:57,440 --> 00:10:58,750 zero floor. 192 00:10:58,750 --> 00:11:00,700 The floor above that is the one floor and the 193 00:11:00,700 --> 00:11:05,300 floor below that, the basement, is the minus one floor. 194 00:11:05,300 --> 00:11:10,198 And so you walk in and you can go either up the elevator or down the elevator. 195 00:11:10,198 --> 00:11:14,240 Of course, in the United States, the floor that you walk in is the 196 00:11:14,240 --> 00:11:19,120 one and then there's the two floor above that and then there's like, the basement. 197 00:11:19,120 --> 00:11:22,460 So this is the imagination that the Americans 198 00:11:22,460 --> 00:11:25,230 have as to how to number floors, right? 199 00:11:25,230 --> 00:11:28,290 The Europeans go zero, one, minus one. 200 00:11:28,290 --> 00:11:34,100 So, children who go to hotels learn instantly the notion of zero and the 201 00:11:34,100 --> 00:11:35,790 notion of positive and negative numbers and 202 00:11:35,790 --> 00:11:38,280 the symmetry between positive and negative numbers. 203 00:11:38,280 --> 00:11:43,520 I mean, I just wish the United States hotels would switch to this 204 00:11:43,520 --> 00:11:48,000 to teach young people zero immediately and 205 00:11:48,000 --> 00:11:49,190 negative numbers. 206 00:11:49,190 --> 00:11:52,730 So we somehow think that numbers all in the United States start at 1 207 00:11:52,730 --> 00:11:56,030 and then there are no negative numbers, there's the 208 00:11:56,030 --> 00:11:56,530 basement. 209 00:11:59,100 --> 00:12:00,990 I wonder why that is, but whatever. 210 00:12:02,630 --> 00:12:06,150 For people who travel a lot, they may be confused by this. 211 00:12:07,190 --> 00:12:08,780 They need a way to convert back and 212 00:12:08,780 --> 00:12:13,009 forth between the US and European numbering system. 213 00:12:14,500 --> 00:12:17,170 So this is a simple program that demonstrates 214 00:12:17,170 --> 00:12:21,160 a real classic pattern of input processing and output. 215 00:12:21,160 --> 00:12:24,380 It's just three lines, but it has the 216 00:12:24,380 --> 00:12:27,940 essential things that all programs that are useful. 217 00:12:27,940 --> 00:12:31,600 They generally read some data, do some work with 218 00:12:31,600 --> 00:12:35,070 the data, and then produce some kind of results. 219 00:12:35,070 --> 00:12:39,740 And so, so the first line is a raw_input 220 00:12:39,740 --> 00:12:44,240 that effectively, that puts out a prompt and then it waits. 221 00:12:44,240 --> 00:12:48,190 It says, please enter your Europe floor. It sits there. 222 00:12:48,190 --> 00:12:49,920 We type a zero, 223 00:12:49,920 --> 00:12:53,100 then zero goes into inp, but it is a string. 224 00:12:54,250 --> 00:12:55,303 It's not a number. 225 00:12:55,303 --> 00:12:56,700 It's a string. 226 00:12:56,700 --> 00:12:59,870 So we can't add to it. But we can take 227 00:13:01,100 --> 00:13:03,800 and convert it to an integer with the int function. 228 00:13:03,800 --> 00:13:06,710 Int of inp, thats a string being converted to an integer 229 00:13:06,710 --> 00:13:09,370 so now its a real numeric zero. 230 00:13:09,370 --> 00:13:13,200 And we can add 1 to that and we sum that together. 231 00:13:13,200 --> 00:13:15,210 And we put it into the 232 00:13:15,210 --> 00:13:19,800 variable usf and then we print US floor, comma, and then 233 00:13:19,800 --> 00:13:24,350 whatever the variable for usf is. And out comes US floor 1. 234 00:13:24,350 --> 00:13:28,280 So we've written a very simple elevator floor conversion 235 00:13:28,280 --> 00:13:31,100 from a European floor to a United States floor. 236 00:13:32,310 --> 00:13:34,810 Don't ask about negative numbers, it's not really good at that. 237 00:13:34,810 --> 00:13:38,120 But from zero and positive numbers it works great. 238 00:13:41,710 --> 00:13:47,130 So another thing to think about in any programming language is comments. 239 00:13:48,580 --> 00:13:53,410 Comments are like commentary, you know, and basically it's a way for us to 240 00:13:54,880 --> 00:13:59,410 add notations for ourselves and for other humans interspersed in the code. 241 00:14:00,480 --> 00:14:05,680 And so in Python anything after a pound sign is ignored. 242 00:14:05,680 --> 00:14:06,900 You can have a pound sign at the beginning 243 00:14:06,900 --> 00:14:08,545 of a line and then the whole line is ignored. 244 00:14:08,545 --> 00:14:11,670 There are two or three reasons why you could do this. 245 00:14:11,670 --> 00:14:14,700 One is sort of like paragraph headings, where you can 246 00:14:14,700 --> 00:14:20,190 say what's going to happen in English or, or your language. 247 00:14:20,190 --> 00:14:22,010 And you can write documentation says this 248 00:14:22,010 --> 00:14:26,510 code was written by Charles Severence, December 2010. 249 00:14:26,510 --> 00:14:28,800 And you can also just hide a line of code to 250 00:14:28,800 --> 00:14:31,920 test and, and turn it on and off without actually deleting 251 00:14:31,920 --> 00:14:35,900 the line of code. It's a real common thing in debugging. 252 00:14:35,900 --> 00:14:42,070 So for example, here is a, here is a, the program that we've been playing with. 253 00:14:42,070 --> 00:14:44,160 This is our word counting program that 254 00:14:44,160 --> 00:14:45,960 we've been talking about from the beginning. 255 00:14:45,960 --> 00:14:50,850 And here is an example of four comments, one, two, three, four. 256 00:14:50,850 --> 00:14:55,330 Four comments that basically tell us what these paragraphs are going to do. 257 00:14:55,330 --> 00:14:58,250 Now, they don't have any effect on the program whatsoever. 258 00:14:58,250 --> 00:15:00,150 But this one says get the name of the file and open it. 259 00:15:01,290 --> 00:15:02,860 Kind of helpful, right? 260 00:15:02,860 --> 00:15:03,910 Count the word frequency. 261 00:15:03,910 --> 00:15:06,870 That's what this little bit does. Find the most common word. 262 00:15:06,870 --> 00:15:08,490 That's what this little bit does. 263 00:15:08,490 --> 00:15:10,500 And all done, print this out. 264 00:15:10,500 --> 00:15:15,450 So it's really can be very helpful just to add a little tiny bit of stuff. 265 00:15:15,450 --> 00:15:17,410 And you don't want to overuse comments. 266 00:15:17,410 --> 00:15:22,470 You don't want to say like x equals 12, take 12 and put it into x. 267 00:15:22,470 --> 00:15:23,530 Sometimes people teach 268 00:15:23,530 --> 00:15:27,360 you and try to say, oh you need a one comment every three lines. 269 00:15:27,360 --> 00:15:28,760 I don't believe in any of those rules. 270 00:15:28,760 --> 00:15:32,230 I basically say if its useful to describe it, then describe it. 271 00:15:33,920 --> 00:15:40,100 So that's comments. So some operators apply to strings. 272 00:15:40,100 --> 00:15:42,410 We've already talked about plus. 273 00:15:42,410 --> 00:15:44,600 It's kind of silly, although useful in places. 274 00:15:44,600 --> 00:15:48,660 You can actually multiply strings. The asterisk looks and 275 00:15:48,660 --> 00:15:53,170 says I've got a string and an integer, and it prints out the string five times. 276 00:15:54,300 --> 00:15:55,120 Not a lot of use for that. 277 00:15:56,620 --> 00:16:00,420 Now, let's talk a little bit about choosing variable names. 278 00:16:00,420 --> 00:16:03,280 This is something that is really confusing. 279 00:16:03,280 --> 00:16:07,170 So I said like, x equals 1, x equals x plus 1, what does x mean? 280 00:16:07,170 --> 00:16:11,190 And the answer is, it doesn't mean anything. 281 00:16:11,190 --> 00:16:13,970 I chose it. I wanted to make a variable, 282 00:16:13,970 --> 00:16:15,770 and so I picked x. 283 00:16:15,770 --> 00:16:18,100 We pick x a lot, probably because we learned 284 00:16:18,100 --> 00:16:22,210 in algebra in sixth grade that x was a variable. 285 00:16:22,210 --> 00:16:25,270 So, and it's short, and so, why not call it x? 286 00:16:28,400 --> 00:16:31,610 But as your programs get larger this gets kind of frustrating 287 00:16:31,610 --> 00:16:34,510 to have all your variables like x and y and z. 288 00:16:34,510 --> 00:16:38,120 And so the notion of mnemonic, it means memory aid. 289 00:16:38,120 --> 00:16:43,150 We choose our variable names wisely, so they remind us of what the variable's 290 00:16:43,150 --> 00:16:48,360 going to do internally. And so, as I go through this lecture 291 00:16:50,540 --> 00:16:54,310 in the beginning if I choose a variable that's too clever 292 00:16:54,310 --> 00:16:58,050 you're going to think that it's part of the language. 293 00:16:58,050 --> 00:17:01,560 And so I sort of switch back and forth between well-chosen variable names 294 00:17:01,560 --> 00:17:06,055 and stupid variable names to kind of reemphasize the notion that I can choose. 295 00:17:06,055 --> 00:17:09,140 Mnemonic is a good practice, okay? 296 00:17:09,140 --> 00:17:12,860 So here we go. Let's take a look at a bit of code. 297 00:17:16,210 --> 00:17:20,100 So the question is, what is this code doing? 298 00:17:20,100 --> 00:17:22,290 What will it even print out? 299 00:17:22,290 --> 00:17:23,890 Is it syntactically correct? 300 00:17:26,380 --> 00:17:32,330 Now you could probably cut and paste this into your brow, into Python and figure 301 00:17:32,330 --> 00:17:38,266 out that it is syntactically correct. There are three variables. 302 00:17:40,266 --> 00:17:45,670 This one here and this one here match. 303 00:17:46,800 --> 00:17:51,860 This one here and that one there match. And these two match. 304 00:17:52,860 --> 00:17:54,360 So it's taking these two numbers and 305 00:17:54,360 --> 00:17:56,890 multiplying together, and then printing out the product 306 00:17:56,890 --> 00:18:02,790 of the two numbers, if you're real careful and like look at every, every character. 307 00:18:02,790 --> 00:18:06,180 Now, this would be called non-mnemonic variables. 308 00:18:06,180 --> 00:18:08,200 They're really messy. 309 00:18:08,200 --> 00:18:12,290 Now Python, it's happy, because all it wants is to say, oh. 310 00:18:12,290 --> 00:18:12,800 Here is then name that 311 00:18:12,800 --> 00:18:15,550 I, the programmer, decided I wanted to call this 312 00:18:15,550 --> 00:18:19,470 piece of memory and I'll refer to it down here, okay? 313 00:18:19,470 --> 00:18:22,320 And so Python is happy. 314 00:18:22,320 --> 00:18:26,280 Now if you hand this to another human being they are going to be really unhappy. 315 00:18:26,280 --> 00:18:28,170 Because they are going to be like, what are you doing? 316 00:18:29,270 --> 00:18:34,660 So one better way to write it would be to make the variables very simple. 317 00:18:34,660 --> 00:18:38,134 And then cognitively we humans can figure out which is which, 318 00:18:38,134 --> 00:18:41,720 because again it's still only about matching. 319 00:18:41,720 --> 00:18:47,470 The a has to match the a, the b matches the b, and the c's match. 320 00:18:47,470 --> 00:18:49,290 It's actually the exact same program. 321 00:18:49,290 --> 00:18:52,570 A equals 35. B equals 12.5. 322 00:18:52,570 --> 00:18:53,820 C equals A times B. 323 00:18:53,820 --> 00:18:56,740 And print C. It is these. 324 00:18:56,740 --> 00:18:59,980 Python sees these as the same program. 325 00:18:59,980 --> 00:19:03,210 It doesn't care what we name them. Now, a human will 326 00:19:03,210 --> 00:19:07,770 be much appreciative if you say, here you can either have this one or this one. 327 00:19:07,770 --> 00:19:10,070 This one will make them a lot happier. 328 00:19:12,450 --> 00:19:13,480 Okay? 329 00:19:13,480 --> 00:19:17,610 So that is certainly cognitively easier, but it's not really 330 00:19:17,610 --> 00:19:22,240 giving you any sense of what's going on here, right? 331 00:19:22,240 --> 00:19:28,260 So an even better way to write this exact same program to do the exact same thing 332 00:19:28,260 --> 00:19:31,430 would be to choose variables named hours, rate, and pay, 333 00:19:31,430 --> 00:19:34,810 if indeed that is what you're doing. 334 00:19:34,810 --> 00:19:37,610 Now you can look at this and you go, oh well, shoot, 335 00:19:37,610 --> 00:19:41,730 35 is the number of hours, and 12.5 is the rate, and the pay is 336 00:19:41,730 --> 00:19:45,090 the number of hours times the rate, and then we are going to print out the pay. 337 00:19:45,090 --> 00:19:47,060 And that makes a lot of sense. 338 00:19:47,060 --> 00:19:53,290 So this is really a awesome and wonderful characterization. 339 00:19:53,290 --> 00:19:55,592 And if that's what you're doing and those are hours, 340 00:19:55,592 --> 00:19:59,090 rate, and pay, it's a great thing to name the variables. 341 00:19:59,090 --> 00:20:03,370 But, this is where beginning students get confused. 342 00:20:03,370 --> 00:20:06,415 And so sometimes I'll write it this way and sometimes I'll write it this way. 343 00:20:06,415 --> 00:20:08,749 Because you'll look at this, until you get a little 344 00:20:08,749 --> 00:20:11,880 more sophisticated, a little more skilled, and you'll say like 345 00:20:14,180 --> 00:20:18,430 does Python know something about payroll? Is hours a reserved word? 346 00:20:18,430 --> 00:20:21,370 Is rate a reserved word and pay a reserved word? 347 00:20:21,370 --> 00:20:25,350 Are these things that Python knows about? And the answer is, no. 348 00:20:25,350 --> 00:20:29,270 Python sees these three programs as exactly the same name. 349 00:20:29,270 --> 00:20:33,440 It's just this person really made a very bad choice of variable name. 350 00:20:33,440 --> 00:20:36,740 This person made a less bad choice of variable name, 351 00:20:36,740 --> 00:20:39,830 and this person made a really awesome choice of variable name. 352 00:20:39,830 --> 00:20:42,010 So the only difference between those two things is style. 353 00:20:44,100 --> 00:20:46,260 They are the exact same program. 354 00:20:46,260 --> 00:20:50,460 And Python is equivalently happy with these, but humans 355 00:20:50,460 --> 00:20:54,280 are most happy when the variables are easy to remember 356 00:20:54,280 --> 00:20:57,840 and they are somewhat descriptive of what their expected contents will be. 357 00:20:58,840 --> 00:21:00,530 That's mnemonic. 358 00:21:00,530 --> 00:21:04,680 To help you remember what you were meaning to do when you write the program. 359 00:21:04,680 --> 00:21:07,280 This is still a bit cryptic, having these 360 00:21:07,280 --> 00:21:09,340 really short, one-character variable names is still 361 00:21:09,340 --> 00:21:11,320 a bit cryptic. So, 362 00:21:13,460 --> 00:21:16,220 You have a couple of assignments at the end of the chapter. 363 00:21:16,220 --> 00:21:19,740 One of the assignments is to write a program to prompt 364 00:21:19,740 --> 00:21:24,220 the user for hours and rate per hour and compute pay. 365 00:21:25,430 --> 00:21:31,566 So, I won't do this here, but just a couple of sort of odd things. 366 00:21:31,566 --> 00:21:33,755 You're going to be using raw_input. 367 00:21:33,755 --> 00:21:38,528 But remember that hands a string in so you're going 368 00:21:38,528 --> 00:21:40,980 to have to use float. 369 00:21:43,030 --> 00:21:44,690 The function to convert it to a floating 370 00:21:44,690 --> 00:21:46,755 point number so you can actually do a calculation. 371 00:21:46,755 --> 00:21:50,170 And then you're going to have to use multiplication and print. 372 00:21:50,170 --> 00:21:52,210 So then multiplication, and then print. 373 00:21:54,200 --> 00:21:58,540 So some combination of raw input, float, multiplication, and print, 374 00:21:59,790 --> 00:22:03,170 constructed to make your program do exactly this. 375 00:22:04,690 --> 00:22:07,350 So, this is the end of Chapter Two. 376 00:22:07,350 --> 00:22:08,210 We talked about types, 377 00:22:08,210 --> 00:22:13,400 reserved words, variables, the mnemonic, how you choose variable names. 378 00:22:13,400 --> 00:22:14,835 We'll hit this a couple more times 379 00:22:14,835 --> 00:22:17,400 because choosing variable names is always problematic. 380 00:22:17,400 --> 00:22:21,400 Operators, operator precedence, which just means like does multiplication happen 381 00:22:21,400 --> 00:22:25,980 before plus, parentheses. Integer division is a little weird because 382 00:22:25,980 --> 00:22:33,255 it truncates, oops, right, 9 over 10. 383 00:22:33,255 --> 00:22:40,050 Oops, 9 over 10 equals 0. That's the integer division truncating. 384 00:22:40,050 --> 00:22:46,930 Conversion, this is like the int() float(). 385 00:22:46,930 --> 00:22:49,030 And then user input, which is raw_input. 386 00:22:49,030 --> 00:22:51,340 And then comments, which are ways for you 387 00:22:51,340 --> 00:22:54,690 to add human-readable text to your program. 388 00:22:54,690 --> 00:22:56,560 Okay? See you next lecture.