From the course: Python for Students (2019)

Creating variables

- A big part of programming is efficiency. Programmers are known for being lazy, and we don't like to do the same thing twice. We especially don't like to do things when we can have our computer do it for us. This is where variables come in. Think of variables like a box. We can put a label on that box to tell us what is inside of it, and we can switch out the content of the box if we want. So, I want to show you how this works. In this example, I have printed some information about my dog. Notice how the word Benny appears twice. What if I wanted to change this to a different dog's name? I would need to change the word Benny in two different places. In this particular example, that's not a huge deal, but you can imagine that if we had a program that was hundreds of lines long or spanned multiple files, it could be pretty error prone to try to find and replace that word across multiple files. Instead, lets use a variable. I'm going to use a variable called dog name, and I'm going to store the name Benny. In Python, declaring a variable is as simple as deciding on a name and using a single equal sign to assign a value to that variable. Now that we have the variable, let's print it to the screen. Save your file, and run it. As you can see, the program is printing out the first string at the top of the file, as well as the name Benny down here. Great, so now we have a variable storing our dog's name, but how do we include it within that string so that we don't have to retype it? For that, we're going to use an F-string. F stands for format. Let's take this string, copy it, delete it, move our information up, and replace this print with our original string. F-strings allow us to embed the variable within the string. Go to the beginning of your string, type F, and then replace the word Benny with curly brackets, and the variable name. Let's do the same thing in the second place that we use it. Save your file, and run it. As you can see, Python has replaced every instance of the variable dog name with the value of the variable dog name which is Benny. There are some naming rules with variables that you should be aware of. Variables can only contain letters, numbers, and underscores. They can not start with a number, and you can not use spaces. You should also avoid Python keywords. One of the most common errors that happens when you first start using variables is misspelling the variable when you try to use it. For example, if I missed the underscore here, let's save it, and run it. Python tells you that the name dog name is not defined. That's because the name of the variable is actually dog underscore name. Be very careful that you're using the exact same naming of your variables, and if you ever encounter a name error, it's a good place to start looking for your mistake. As our programs get more complex, variables are going to become very important. Go ahead and try to create some more variables. Can you create a variable containing your name? What about your age? Can you print a message outputting them? Can you print your name in uppercase, lower case, and title case? Don't forget, if you get stuck, Google is your best friend.

Contents