0:06

Welcome to class.

Â Last lecture we talked about arithmetic expressions.

Â And we know that the we could use the rules of precedence to actually compute a

Â value for that arithmetic expression. Now in this lecture we're going to show

Â that we can save that value in a variable. We're going to assign a name to that

Â variable and then we can use that variable once subsequent arithmetic expressions.

Â Why we want to do that? Well there's a couple reasons.

Â First, if the expression was big and involved a complicated calculation, we

Â might not want to actually do that again. So saving that value in a name avoids

Â computing the same expression over and over.

Â Second thing is, by giving this value a name we can help the person that's looking

Â at your code understand what your computation, your program is doing.

Â So, I'm going to walk you through a few examples of using variables inside your

Â program to basically make your program more efficient or make it more

Â understandable. Okay, let's do some examples.

Â 1:17

So let's talk about variables. So a variable is a placeholder for storing

Â a value. Reclaim and store it to avoid

Â re-computation, or to give a value a name to help understand what it represents.

Â In Python valid variable names consist of combinations of letters, numbers and the

Â underscore character. This character right here.

Â The name has to start with either a letter or a underscore.

Â We'll talk about when you wanna use underscore later in the class.

Â The variable names are case sensitive. Typically, for now you should start with

Â lowercase, then we'll tell you when you use uppercase conventionally later in the

Â class. Here's some examples of a, ninja, very

Â nice variable name. Capital ninja.

Â Pretty good variable name also but don't use it until later in the class.

Â Ninja underscore, underscore, underscore, underscore.

Â Great gamer name, maybe not so great Python name.

Â Illegal names. A number.

Â 1337. That actually has a meaning, that is

Â called leet speak. One as in L, three as in E, E, seven as a

Â T, leet. You can't be a leet, ninja unfortunately,

Â at least not using numbers. If you want to have a multiple word name,

Â variable name, it's fine. Just connect all the words using an

Â underscore, that's python convention. So, for example, a legal name is elite

Â ninja. If you want that to be even more elite you

Â could be leet ninja. Or if you want to quantify your ninjaness,

Â you can say, ninja<u>elite.elite. Unfortunately, you still can't start with</u>

Â the number, so this is not going to work. How do you actually take that value and

Â assign it to a variable? Well, you use equals.

Â This is the same thing you did in, say middle school algebra.

Â You say, variable equal value. Now notice that if you want to test to see

Â if two values are equal you use a double equal.

Â So, single equal is assignment, double equal is equality testing.

Â Now, probably the most critical thing that you need to do whenever you want to go

Â through and actually choose variable names is to think about something that's

Â memorable that will help you understand what the variable represents.

Â So, for example, if I will go through and I say up here, I say M.

Â Variable M, what does it represent? Not sure but if I say my underscore name,

Â I bet you can guess what that's going to be.

Â It's going to be Joe Warren. So I can ask to print that out.

Â Print my name, run that. Sure enough it's Joe Warren.

Â I get another variable. I can say, my age. My age, number is 51.

Â 4:04

Okay. So, we've assigned some values to some

Â variables, what can we do with them? Well, birthdays are fun.

Â I just, I actually had a birthday in February.

Â So, next February, I'll have another one. So, what could I do?

Â I could go through and update my age. How would I do that?

Â Well, I could say something like my age is equal to 51 plus one, then I could print

Â my age. So if I run that.

Â It's gonna be 52, surprise, surprise. But notice this is kind of a foolish

Â expression here. Because I already had, okay, my current

Â age right up here in this variable my age. So, in fact, what I should have really

Â said is something like, my age is = to my age +one.

Â And notice, that now works no matter what my current age.

Â This is always going to give me one more than the current age.

Â Computations like this, where I take a variable, I do something to it.

Â And I update that second variable, are actually so frequent.

Â But there's a shorthand that you can use in Python, and again, lots of other

Â languages. Where you can use an operator which is

Â called plus =. So the plus = operator takes the thing on

Â the left hand side, gets its value. Takes this operator and applies it to the

Â right hand side. And then stuffs the value back into the

Â left hand side. So this does exactly the same thing.

Â And then it comes back with my age is equal to 52.

Â Let's see, what's another one? Let's do one more example real quick.

Â So I'm going to tell you a story about another variable and this one may not be,

Â seem to be exactly as understandable to begin with, but I'm going to I'm going to

Â have a variable called magic pill. And I'm going to print out.

Â 5:47

My age minus the magic pill. So if I do that.

Â Come back with, well, 22. Alright, so let's go through and comment

Â out this, so we get the correct age. Get 21.

Â So, what is magic pill? So, let me quick, quickly tell you the

Â story of the magic pill. So, I have three children seventeen,

Â fifteen and twelve. And, my fifteen year old, perhaps at one

Â point, that he was going to go through and invent a pill that took 30 years off your

Â age. So, he was going to give it away.

Â So my oldest son said, wow, you know, that's, that's not wise, you should sell

Â that pill, you know, we could make a lot of money.

Â So, we had a long discussion about what it would, the value of a pill that took 30

Â years off your age would be. And so we decided on $300,000.

Â And so, there's still some doubt from my fifteen year old that this was actually

Â really worth $300,000, so he called his granddad.

Â Now his granddad was. 74.

Â And he asked grandad, would you, would you pay $300,000 for a pill that took 30 years

Â off your age. And so grandad had an interesting

Â response, thought for a little bit, and he said, I'll take two of those pills.

Â So, let's print out what would happen if grandad bought two pills.

Â 7:18

B14, I think it's a wise choice. So, give your variable names memorable

Â names, give your variables memorable names, it'll help you when you go back and

Â look at your program and other people go back and look your program to understand

Â what's going on. Okay.

Â Give me a sec. So let's finish up with a more serious

Â example that does something useful. So here I have in my comments, we'd like

Â to convert from Fahrenheit to Celsius. So I've actually written down a formula

Â here that kind of describes the conversion from a temperature F in Fahrenheit, to a

Â temperature C in Celsius. So let's just turn that into a python

Â code. So what I'm gonna do is, I'm gonna define

Â a value for the Fahrenheit. So I'm gonna be a little more deliberative

Â here and actually give it a longer name. I'm gonna call it Temp Fahrenheit.

Â And I'm gonna initialize it to say, let's make it 32.

Â I think I know what the value of 32 Fahrenheit is in Celsius.

Â And so then what I need to do? I want to go through my expression that

Â converts ten Fahrenheit into temperature in Celsius.

Â So I can say ten Celsius Is equal to, let's see, five nights.

Â Times, well what are we going to use, it's going to be the temperature in Fahrenheit.

Â So it's tempFahrenheit. Minus 32 and then let's print out what the

Â resulting temperature is. So we'll print temp.

Â 8:59

Now, one thing it's often good to do is, when you type in a piece of code and

Â you're testing it. Instead of just typing values, and then

Â kind of running it, and hoping that things come out right.

Â It's often good to think, okay. What should the answer be before you run

Â the code? So I said I knew the value of 32 degrees

Â Fahrenheit and Celsius. And yes, that's zero degrees Celsius,

Â that's freezing. So let's see what comes out here if I run

Â it. So, good.

Â Came out 32 Fahrenheit is zero degrees Celsius.

Â Now, we also know that, let's see, 212 degrees Fahrenheit should be 100 degrees

Â Celsius. So the value of writing this expression

Â down here is that now we can just go through, and change the value of

Â Fahrenheit to be 212. And run it again, and if we're doing well,

Â it comes out to be 100. Then let's do it the other way.

Â Let's go through and write an expression that converts from Celsius to Fahrenheit,

Â so you get one more little piece of practice.

Â So I could say, Tip Celsius is equal to zero, and then I can write my expression

Â that converts from Celsius to Fahrenheit. So, temp Fahrenheit is equal to, well,

Â let's see what my formula says, it says 9/5, times, well, Tip Celsius.

Â 10:18

Plus, 32. Let's make it print out.

Â Print, Temp Fahrenheit So again, if the, temperature is zero degrees Celsius, we'd

Â expect 32 degrees Fahrenheit. So sure enough it worked.

Â Let's just do one more test real quick, it's always good to at least do a couple

Â of tests when building things. So if his temperature is 100 degrees

Â Celsius that's boiling so that should be 212 degrees Fahrenheit, so sure enough it

Â worked. So those are a couple of examples of using

Â variables to organize your computations. Now in our next lecture we talk about more

Â programming in Python, Scott's going to talk about functions and he'll actually

Â come back and revisit this example. I'll see you in a few more lectures.

Â