Using Python Variables

You don't have to use hard-coded numbers in your code. You can substitute them for variables instead.

Either delete the code you have or comment out the lines with the # symbol. (Sadly, Python doesn't have a shortcut for multiline comments. There is a much-debated workaround using three quotes marks, but we won't use them.)

Now set up the following two variables:

numberOne = 16
numberTwo = 4

So we're just storing a value of 16 in the variable called numberOne, and a value of 4 in the variable called numberTwo.

We can add these two numbers together. Hit the enter key on your keyboard and add the following line:

answer = numberOne + numberTwo

Notice that as soon as you start to type the variable name numberOne, you'll get a popup box:

Python code completetion in PyCharm

This is Python telling you that it recognises your variable name and that you can tap the Tab key on your keyboard to complete the code (or double click an entry on the list).

Now type a print statement (again, use the Tab key to complete code):

print("The answer is:", answer)

Your coding window should look like this:

Adding two Python variables together

Run your code to see the output:

Python output window showing result of addition

So, what happened? Well, Python recognised the two number variables you set up, and took note of what was inside of each of them:

Two Python variables and the values they store

Remember: Python will do the calculations to the right of the equals sign first. When it finds an answer, it stores that answer into whatever variable you have on the left of the equals sign. So Python sees that there is 16 stored in the numberOne variable and 4 stored in the numberTwo variable. It adds these up and stores the result in the answer variable.

You can also mix the two, variables and hard-code numbers:

answer = numberOne + numberTwo + 20
answer = 10 * numberOne + (numberTwo + 20)
answer = numberOne + 20 + numberTwo - 10

Another thing to bear in mind is the values in variables can change. Take this code as an example:

numberOne = 16
numberTwo = 4
answer = numberOne + numberTwo
print("The answer is:", answer)
numberOne = 10
answer = numberOne + numberTwo
print("The second answer is:", answer)

Here, we have two assignments to numberOne. The second is in bold and assigns a value of 10 to numberOne. The previous contents of numberOne will be erased. So 16 will get wiped out. A value of 10 takes its place.

You can retain a value in a variable. This is a common programming technique and one that's very useful to learn. What you do is to repeat the variable name on the right-hand side of the equals sign. Like this:

answer = answer + 1

To understand this, remember to start on the right of the equals sign. Then do the calculation. So whatever is currently inside of the variable answer, which was 14 in the code above, will get 1 added to it. Once that has been calculated, Python will then assign this new total back into the variable called answer, making the new answer 15. This is called incrementing a variable.

Setting up Python variables on the same line

You don't have to set up and assign variables on separate lines. You can set them up on the same line. For example, in our code above, we could have done this:

numberOne, numberTwo = 16, 4

Here, we have two variable names separated by commas. Next comes the equals sign. After the equals sign, you type the values that are going into your variable names.

Again, a comma separates the values. You can set up as many variables and values you want on the same line. Just remember to insert the commas in the right place:

numberOne, numberTwo, numberThree = 16, 4, 25
answer = numberOne + numberTwo + numberThree
print("The answer is:", answer)

Try the code out and see what answer you get.

In the next lesson, you'll learn how to display things in a message box. We'll then move on to String variables.

Message Boxes >