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