2.2 Work with variables - Video Tutorials & Practice Problems
Video duration:
12m
Play a video:
<v ->So now that we've seen the fundamental concept of types.</v> We're now going to look at variables, and we've already looked at assigning variables, but now I'll show you more about how to reassign them, delete them, and change them etc. So if you remember, assigning a variable looks like this, x equals 5. So I've given the variable a name called x, and I've given it the value of five. And I can now see what that variable is, it's five and actually in PyCharm in the console, we have a little side bar here that tells you the current value of different variables. I can reassign that variable, called and make it six. You can see that it changed to six. I can change it based on its current value. So x equals x plus one. And now it's seven. And as a short form of that I can also say x plus equals one. And now its eight. So you can use x minus equals, as well and any number that you want, 92. As you can see, I didn't have to say that x was gonna hold an integer. And if I wanted to I could make it not an integer, I could make it a float. I can also make it a string if I wanted to. Monday. Even though Python knows in the back end that these different pieces of data have different types, you don't actually have to explicitly say what the type is going to be. So if I did something like x equals seven, x plus five, that equals 12. And then if I changed x to equal to the string Monday, and then did x plus five. Now it'll give me an error. So TypeError must be string not int. This is not a very useful error, but it's at least saying that there's something related to the types not matching. So I'm not gonna talk to you much about like whether this was a good decision or a bad decision on Python's part. It does make the code easier to read and in some ways less like nit picky. And so less error prone for beginners. But it's a thing to think about. What type the object is that a variable holds. So yeah going back high level to analogy mode. I apologize if you don't like analogy's, some people do. So I like to think of Variables as boxes. And say you are moving a house and you have a bunch of boxes to store stuff in. You can put all of your dishes into a box and label it dishes, and that's well and good. You move it to the next house, open it up and there are dishes like you expect. You can also put more dishes in the box, take more dishes out of the box. And only have a few in there, that your going to give away. You can empty it completely. You can also throw whatever you want in there. You can throw shoes in there. But it'll still be labeled dishes. And so this is kinda how Python treats variables, just as a container with a label. So in terms of naming variables, each variable can have any number of letters, numbers, and underscores. So it has to be all one word. But it can not start with a number. It's also case sensitive. So if you had one variable capitalized and then tried to use it again later on and it wasn't capitalized, the computer will be like I have no idea what you mean. You know computers are very literal. And then because of this like loose typing, the preference in Python is to have readable, maybe slightly longer variable names and function names, that are more descriptive. Especially more descriptive than just single letters. Which are common in languages like c, or you know just in school. So we would prefer name instead of n, or like first name, or username, things like that. There are also some words that you can't use as variable names in Python. Here is a list of keywords in the Python programming languages and they are reserved for other uses. So for example, true and false, are on this list. You can not redefine what true and false mean, because those are like core types in Python. There are also this list of Built-in Functions, and while you can name variables with these names, it's not recommended and I will show you why. Okay, so first of all let's try assigning true equals four. Syntax Error can't assign to keyword. So this is a keyword, it's special, and I can't reassign it. I'll show you that I can name a variable, my variable four, but I can not call it four my variable. It can not start with a number. And then it also can't have any spaces in it. Cause it treats these as two different objects, two different words. Okay, so now let's look at trying to assign a built-in function. Like the print function, that we've seen already, to a variable. So print equals four. That's cool, it lets me do that. I can check out what print is, can I print what print is. No, because this is now, an int object and it's not callable, which means it's not a function and I can't use these parentheses after it. So that's not good, I no longer have access to that built-in function, that I was trying to use. So I made a mistake, I want to get rid of that variable assignment. I can use del, the del function, to delete that reference. So now print is no longer a variable and if I say print I can see that it's a built-in function. Oh yeah, also let's look at what the type of print is. Built-in function or method, cool. So functions are objects too, just with a special type. Okay so looking back at this built-in function list, you can say we have print here. And I also want to show you this other function called input because we're gonna need it for our next problem. Okay so I can refresh to clear it all up. So input is a function, and let's see what happens if I press enter it gives me this like one arrow and a question mark, and I can type what I want into it. And it'll spit it back out. If I use the function and pass in a string, call like, enter your name. Then I can type it in here, and then it prints out what I typed in afterwards. So this is because input returns this value, which is what I typed in. And we can save that value now to a variable, and I can call it name. And then now, I can type my name in, and you can see that name has been saved as a variable now. I can check what it's called, I can also add it to other strings. So I can say print, hello, and then plus name. So this is something called string concatenation. I'm taking two strings and smushing them together. And if I don't include that space, then it doesn't include the space afterwards, it's very literal. So now these are going to help us with this next problem. Which is singing Happy Birthday to somebody. You can open up problem two, Happy birthday. And say, you know maybe you've got a friends birthday, whose pretty nerdy, you can give them this Python file and get them to run it. And it'll sing Happy Birthday to them. First we want to ask for a users name. So, input what's you name. And since I have an apostrophe in what's, I'm using these double quotes, instead of only single quotes on the outside. So Python doesn't get confused about what's part of the string and what's not. Okay, and then I'm gonna take that input and save it to another variable called name. It can also be whatever you want it to be called as long as your consistent when you use it. It could be birthday person. And then, you know I could say like, Happy birthday, name. And let's run this, right click and run. And nothing prints out afterwards. And that's because when I'm running a file, the only things that print out are the ones I specifically tell it to print out. So I have to use the print function. So that's different from using the console. Where it will print whatever the last thing was that was evaluated. So if I now do this, I can, I just reran it, and press enter. And now it says Happy birthday Arianne, that's cool. So you can, you know, do this in whatever language you want. And whatever birthday song you want. If I want to do multiple lines, I can just print multiple times. So I'll copy that. And do Happy birthday to you, copy it, paste it, paste it. And then, run it. Oh yay, thank you! Yeah, so that's it for this section about variables.