Introduction
Imagine you’re programming a video game. You have a cute little monster character on the screen who can run left or right. Inside your program, you keep track of where the character is standing, using a number. Smaller numbers are more to the left, larger numbers are more to the right:
How will your program keep track of every location where the little monster character can be? How will you change the program when the number changes? We need a number that is allowed to change while the program is running.
What is a variable?
Programming solves this problem by stealing a concept from algebra: variables.
Variables are pieces of information that are allowed to change. Let’s call the little monster’s position x. So in the first picture, x is 40, and in the second picture, x is 60. x is a variable. It changes as the little monster moves.
A number like 5.0 or 3.5 is constant, meaning it can never change. But variables like x can change.
Open a new file in Sublime text editor, and write this code:
1 2 |
x = 50 print(x) |
Then, save your file, open a terminal and run your file in python. For example, if you saved your file as “variables_part1.py”, run:
1 |
python variables_part1.py |
Your program should print the output:
50
So far not so different from what we’ve seen before, right?
In line 1, we created a variable called x and gave it the value 50. This is what’s called assignment equals. The equals sign here doesn’t mean x is permanently set to 50 (like it would in regular math). It means x gets assigned the value 50.
From this point on, whenever Python encounters x, it will use the value 50. This is what happens in line 2: when Python sees the statement print(x)
, it looks up the value of x, which is 50, and so it prints 50.
Now, modify your program to the following, and run it again:
1 2 3 4 |
x = 50 print(x) x = 35 print(x) |
Your program should print the output:
50
35
What happened?! The first time x was printed, it had the value 50. The second time, it had the value 35.
Let’s break this down line by line.
In line 1, we make a new variable called x and give it the value 50, using assignment equals again. In line 2, when Python sees print(x)
, it looks up the value of x, which is 50, and prints 50.
In line 3, when Python sees x, it doesn’t create a new variable (because x already exists). It just assigns the value 35. From now on in this program, the value of x will be 35.
So in line 4, when Python sees print(x)
again, it looks up the value of x, which is now 35, and so it prints 35.
Calculations with variables
We use variables to make code easy to read. If we have a calculation which gets complicated, it can be really useful to break it down into parts and label those parts.
The following program calculates the tip on a restaurant bill. Open a new file in Sublime text editor, and write this code:
1 2 3 4 |
bill = 15.34 + 2.30 + 9.85 + 3.02 + 6.99 tip = bill * 0.15 print(tip) |
Then, save your file, open a terminal and run your file in Python. For example, if you saved your file as “variables_part2.py”, run:
1 |
python variables_part2.py |
Your program should print the output:
5.625
In line 1, we create a new variable called bill, and we add several prices together to come up with a value for bill.
In line 2, we create another new variable called tip, and assign it the value of bill * 0.15 (multiplying by 0.15 is the same as calculating 15%). We can use bill in the calculation because Python has already seen it and given it a value.
Line 3 is a blank line. That’s allowed!
Line 4 prints the value of tip.
Note that there’s nothing special about the names bill and tip. We could have given the variables any names we want, like elephant and giraffe, or bill and ted, or apple and surfboard. As long as the names are consistent, Python doesn’t care. The first time bill is created, Python makes a note that there is a variable called bill. Any time it sees bill in the code, it looks to see if there is already a variable called bill.
Note: one of the primary goals of the Python language is to create code that is easily readable to humans. So, even though we can name our variables anything we want, that doesn’t mean we should. As programmers who might work collaboratively on projects, we should try and pick variable names that are clear and make sense in the context of the code we write.
For a challenge, try changing the hard coded numbers in the program above. Calculate a 20% tip. Add an extra item costing $10.99 to the bill and see how that changes the result.
1 2 3 4 |
bill = 15.34 + 2.30 + 9.85 + 3.02 + 6.99 + 10.99 tip = bill * 0.20 print(tip) |
Run your program by typing:
1 |
python variables_part3.py |
The output should be: 9.698
Conclusion
Hopefully you now understand number variables in Python. Numbers are only one type of variable. In the next post, we will learn about strings of text.