1 00:00:00,000 --> 00:00:01,965 . Welcome to class. 2 00:00:02,201 --> 00:00:06,289 Last lecture we talked about arithmetic expressions. 3 00:00:06,289 --> 00:00:12,579 And we know that the we could use the rules of precedence to actually compute a 4 00:00:12,579 --> 00:00:18,397 value for that arithmetic expression. Now in this lecture we're going to show 5 00:00:18,397 --> 00:00:24,372 that we can save that value in a variable. We're going to assign a name to that 6 00:00:24,372 --> 00:00:30,820 variable and then we can use that variable once subsequent arithmetic expressions. 7 00:00:30,820 --> 00:00:33,991 Why we want to do that? Well there's a couple reasons. 8 00:00:33,991 --> 00:00:38,598 First, if the expression was big and involved a complicated calculation, we 9 00:00:38,598 --> 00:00:43,324 might not want to actually do that again. So saving that value in a name avoids 10 00:00:43,324 --> 00:00:45,957 computing the same expression over and over. 11 00:00:45,957 --> 00:00:51,226 Second thing is, by giving this value a name we can help the person that's looking 12 00:00:51,226 --> 00:00:55,945 at your code understand what your computation, your program is doing. 13 00:00:55,945 --> 00:01:01,218 So, I'm going to walk you through a few examples of using variables inside your 14 00:01:01,218 --> 00:01:06,076 program to basically make your program more efficient or make it more 15 00:01:06,076 --> 00:01:09,060 understandable. Okay, let's do some examples. 16 00:01:11,360 --> 00:01:16,515 So let's talk about variables. So a variable is a placeholder for storing 17 00:01:16,515 --> 00:01:19,145 a value. Reclaim and store it to avoid 18 00:01:19,145 --> 00:01:24,583 re-computation, or to give a value a name to help understand what it represents. 19 00:01:24,790 --> 00:01:29,836 In Python valid variable names consist of combinations of letters, numbers and the 20 00:01:29,836 --> 00:01:32,728 underscore character. This character right here. 21 00:01:32,912 --> 00:01:36,543 The name has to start with either a letter or a underscore. 22 00:01:36,543 --> 00:01:40,604 We'll talk about when you wanna use underscore later in the class. 23 00:01:40,789 --> 00:01:45,774 The variable names are case sensitive. Typically, for now you should start with 24 00:01:45,774 --> 00:01:50,758 lowercase, then we'll tell you when you use uppercase conventionally later in the 25 00:01:50,758 --> 00:01:53,589 class. Here's some examples of a, ninja, very 26 00:01:53,589 --> 00:01:55,620 nice variable name. Capital ninja. 27 00:01:55,620 --> 00:02:00,737 Pretty good variable name also but don't use it until later in the class. 28 00:02:00,737 --> 00:02:04,104 Ninja underscore, underscore, underscore, underscore. 29 00:02:04,104 --> 00:02:07,337 Great gamer name, maybe not so great Python name. 30 00:02:07,539 --> 00:02:09,290 Illegal names. A number. 31 00:02:09,290 --> 00:02:11,944 1337. That actually has a meaning, that is 32 00:02:11,944 --> 00:02:15,635 called leet speak. One as in L, three as in E, E, seven as a 33 00:02:15,635 --> 00:02:18,678 T, leet. You can't be a leet, ninja unfortunately, 34 00:02:18,678 --> 00:02:23,275 at least not using numbers. If you want to have a multiple word name, 35 00:02:23,275 --> 00:02:27,159 variable name, it's fine. Just connect all the words using an 36 00:02:27,159 --> 00:02:31,886 underscore, that's python convention. So, for example, a legal name is elite 37 00:02:31,886 --> 00:02:34,735 ninja. If you want that to be even more elite you 38 00:02:34,735 --> 00:02:38,641 could be leet ninja. Or if you want to quantify your ninjaness, 39 00:02:38,641 --> 00:02:42,494 you can say, ninjaelite.elite. Unfortunately, you still can't start with 40 00:02:42,494 --> 00:02:47,407 the number, so this is not going to work. How do you actually take that value and 41 00:02:47,581 --> 00:02:50,137 assign it to a variable? Well, you use equals. 42 00:02:50,137 --> 00:02:53,795 This is the same thing you did in, say middle school algebra. 43 00:02:53,795 --> 00:02:57,977 You say, variable equal value. Now notice that if you want to test to see 44 00:02:57,977 --> 00:03:00,591 if two values are equal you use a double equal. 45 00:03:00,591 --> 00:03:04,250 So, single equal is assignment, double equal is equality testing. 46 00:03:04,250 --> 00:03:09,045 Now, probably the most critical thing that you need to do whenever you want to go 47 00:03:09,045 --> 00:03:13,663 through and actually choose variable names is to think about something that's 48 00:03:13,663 --> 00:03:17,749 memorable that will help you understand what the variable represents. 49 00:03:17,749 --> 00:03:21,420 So, for example, if I will go through and I say up here, I say M. 50 00:03:21,420 --> 00:03:28,272 Variable M, what does it represent? Not sure but if I say my underscore name, 51 00:03:28,272 --> 00:03:32,293 I bet you can guess what that's going to be. 52 00:03:32,293 --> 00:03:38,973 It's going to be Joe Warren. So I can ask to print that out. 53 00:03:38,973 --> 00:03:45,016 Print my name, run that. Sure enough it's Joe Warren. 54 00:03:45,371 --> 00:03:52,600 I get another variable. I can say, my age. My age, number is 51. 55 00:03:58,340 --> 00:04:02,203 Okay. So, we've assigned some values to some 56 00:04:02,203 --> 00:04:07,415 variables, what can we do with them? Well, birthdays are fun. 57 00:04:07,415 --> 00:04:11,459 I just, I actually had a birthday in February. 58 00:04:11,459 --> 00:04:16,671 So, next February, I'll have another one. So, what could I do? 59 00:04:16,671 --> 00:04:21,793 I could go through and update my age. How would I do that? 60 00:04:21,793 --> 00:04:29,430 Well, I could say something like my age is equal to 51 plus one, then I could print 61 00:04:29,430 --> 00:04:31,212 my age. So if I run that. 62 00:04:31,212 --> 00:04:36,092 It's gonna be 52, surprise, surprise. But notice this is kind of a foolish 63 00:04:36,092 --> 00:04:39,752 expression here. Because I already had, okay, my current 64 00:04:39,752 --> 00:04:45,106 age right up here in this variable my age. So, in fact, what I should have really 65 00:04:45,106 --> 00:04:48,360 said is something like, my age is = to my age +one. 66 00:04:48,840 --> 00:04:51,572 And notice, that now works no matter what my current age. 67 00:04:51,572 --> 00:04:54,451 This is always going to give me one more than the current age. 68 00:04:54,597 --> 00:04:57,915 Computations like this, where I take a variable, I do something to it. 69 00:04:57,915 --> 00:05:00,794 And I update that second variable, are actually so frequent. 70 00:05:00,794 --> 00:05:04,454 But there's a shorthand that you can use in Python, and again, lots of other 71 00:05:04,454 --> 00:05:06,845 languages. Where you can use an operator which is 72 00:05:06,845 --> 00:05:09,480 called plus =. So the plus = operator takes the thing on 73 00:05:09,480 --> 00:05:13,188 the left hand side, gets its value. Takes this operator and applies it to the 74 00:05:13,188 --> 00:05:15,921 right hand side. And then stuffs the value back into the 75 00:05:15,921 --> 00:05:18,410 left hand side. So this does exactly the same thing. 76 00:05:18,410 --> 00:05:21,309 And then it comes back with my age is equal to 52. 77 00:05:21,483 --> 00:05:25,311 Let's see, what's another one? Let's do one more example real quick. 78 00:05:25,311 --> 00:05:29,950 So I'm going to tell you a story about another variable and this one may not be, 79 00:05:29,950 --> 00:05:34,648 seem to be exactly as understandable to begin with, but I'm going to I'm going to 80 00:05:34,648 --> 00:05:39,920 have a variable called magic pill. And I'm going to print out. 81 00:05:41,660 --> 00:05:50,480 My age minus the magic pill. So if I do that. 82 00:05:50,880 --> 00:05:56,254 Come back with, well, 22. Alright, so let's go through and comment 83 00:05:56,254 --> 00:06:00,588 out this, so we get the correct age. Get 21. 84 00:06:00,588 --> 00:06:03,865 So, what is magic pill? So, let me quick, quickly tell you the 85 00:06:03,865 --> 00:06:07,142 story of the magic pill. So, I have three children seventeen, 86 00:06:07,142 --> 00:06:10,364 fifteen and twelve. And, my fifteen year old, perhaps at one 87 00:06:10,364 --> 00:06:15,029 point, that he was going to go through and invent a pill that took 30 years off your 88 00:06:15,029 --> 00:06:17,029 age. So, he was going to give it away. 89 00:06:17,029 --> 00:06:21,139 So my oldest son said, wow, you know, that's, that's not wise, you should sell 90 00:06:21,139 --> 00:06:23,805 that pill, you know, we could make a lot of money. 91 00:06:23,805 --> 00:06:28,304 So, we had a long discussion about what it would, the value of a pill that took 30 92 00:06:28,304 --> 00:06:31,470 years off your age would be. And so we decided on $300,000. 93 00:06:31,470 --> 00:06:37,053 And so, there's still some doubt from my fifteen year old that this was actually 94 00:06:37,053 --> 00:06:40,334 really worth $300,000, so he called his granddad. 95 00:06:40,334 --> 00:06:47,933 Now his granddad was. 74. 96 00:06:47,933 --> 00:06:54,942 And he asked grandad, would you, would you pay $300,000 for a pill that took 30 years 97 00:06:54,942 --> 00:06:59,164 off your age. And so grandad had an interesting 98 00:06:59,164 --> 00:07:05,497 response, thought for a little bit, and he said, I'll take two of those pills. 99 00:07:05,497 --> 00:07:11,240 So, let's print out what would happen if grandad bought two pills. 100 00:07:12,500 --> 00:07:16,880 B14, I think it's a wise choice. So, give your variable names memorable 101 00:07:16,880 --> 00:07:21,832 names, give your variables memorable names, it'll help you when you go back and 102 00:07:21,832 --> 00:07:27,038 look at your program and other people go back and look your program to understand 103 00:07:27,038 --> 00:07:28,371 what's going on. Okay. 104 00:07:28,371 --> 00:07:33,923 Give me a sec. So let's finish up with a more serious 105 00:07:33,923 --> 00:07:39,083 example that does something useful. So here I have in my comments, we'd like 106 00:07:39,083 --> 00:07:44,415 to convert from Fahrenheit to Celsius. So I've actually written down a formula 107 00:07:44,415 --> 00:07:50,087 here that kind of describes the conversion from a temperature F in Fahrenheit, to a 108 00:07:50,087 --> 00:07:54,393 temperature C in Celsius. So let's just turn that into a python 109 00:07:54,393 --> 00:07:57,537 code. So what I'm gonna do is, I'm gonna define 110 00:07:57,537 --> 00:08:02,322 a value for the Fahrenheit. So I'm gonna be a little more deliberative 111 00:08:02,322 --> 00:08:07,380 here and actually give it a longer name. I'm gonna call it Temp Fahrenheit. 112 00:08:07,840 --> 00:08:11,392 And I'm gonna initialize it to say, let's make it 32. 113 00:08:11,392 --> 00:08:15,558 I think I know what the value of 32 Fahrenheit is in Celsius. 114 00:08:15,558 --> 00:08:20,340 And so then what I need to do? I want to go through my expression that 115 00:08:20,340 --> 00:08:23,892 converts ten Fahrenheit into temperature in Celsius. 116 00:08:23,892 --> 00:08:32,720 So I can say ten Celsius Is equal to, let's see, five nights. 117 00:08:33,180 --> 00:08:37,909 Times, well what are we going to use, it's going to be the temperature in Fahrenheit. 118 00:08:37,909 --> 00:08:45,387 So it's tempFahrenheit. Minus 32 and then let's print out what the 119 00:08:45,387 --> 00:08:50,000 resulting temperature is. So we'll print temp. 120 00:08:53,340 --> 00:08:57,379 Now, one thing it's often good to do is, when you type in a piece of code and 121 00:08:57,379 --> 00:09:00,409 you're testing it. Instead of just typing values, and then 122 00:09:00,409 --> 00:09:03,439 kind of running it, and hoping that things come out right. 123 00:09:03,439 --> 00:09:07,213 It's often good to think, okay. What should the answer be before you run 124 00:09:07,213 --> 00:09:09,871 the code? So I said I knew the value of 32 degrees 125 00:09:09,871 --> 00:09:13,007 Fahrenheit and Celsius. And yes, that's zero degrees Celsius, 126 00:09:13,007 --> 00:09:16,090 that's freezing. So let's see what comes out here if I run 127 00:09:16,090 --> 00:09:16,839 it. So, good. 128 00:09:16,839 --> 00:09:20,301 Came out 32 Fahrenheit is zero degrees Celsius. 129 00:09:20,301 --> 00:09:26,194 Now, we also know that, let's see, 212 degrees Fahrenheit should be 100 degrees 130 00:09:26,194 --> 00:09:29,730 Celsius. So the value of writing this expression 131 00:09:29,730 --> 00:09:34,813 down here is that now we can just go through, and change the value of 132 00:09:34,813 --> 00:09:39,294 Fahrenheit to be 212. And run it again, and if we're doing well, 133 00:09:39,294 --> 00:09:43,451 it comes out to be 100. Then let's do it the other way. 134 00:09:43,451 --> 00:09:50,093 Let's go through and write an expression that converts from Celsius to Fahrenheit, 135 00:09:50,093 --> 00:09:53,738 so you get one more little piece of practice. 136 00:09:53,738 --> 00:10:00,137 So I could say, Tip Celsius is equal to zero, and then I can write my expression 137 00:10:00,137 --> 00:10:06,374 that converts from Celsius to Fahrenheit. So, temp Fahrenheit is equal to, well, 138 00:10:06,374 --> 00:10:11,720 let's see what my formula says, it says 9/5, times, well, Tip Celsius. 139 00:10:12,720 --> 00:10:15,820 Plus, 32. Let's make it print out. 140 00:10:15,820 --> 00:10:26,090 Print, Temp Fahrenheit So again, if the, temperature is zero degrees Celsius, we'd 141 00:10:26,090 --> 00:10:30,413 expect 32 degrees Fahrenheit. So sure enough it worked. 142 00:10:30,413 --> 00:10:35,258 Let's just do one more test real quick, it's always good to at least do a couple 143 00:10:35,258 --> 00:10:39,316 of tests when building things. So if his temperature is 100 degrees 144 00:10:39,316 --> 00:10:44,281 Celsius that's boiling so that should be 212 degrees Fahrenheit, so sure enough it 145 00:10:44,281 --> 00:10:47,309 worked. So those are a couple of examples of using 146 00:10:47,309 --> 00:10:52,336 variables to organize your computations. Now in our next lecture we talk about more 147 00:10:52,336 --> 00:10:57,120 programming in Python, Scott's going to talk about functions and he'll actually 148 00:10:57,120 --> 00:11:01,420 come back and revisit this example. I'll see you in a few more lectures.