Intro to Python

3. Variables

The vast majority of applications, regardless of platform, store data. Variables are one such method of storing data, and they are helpful because an application can constantly manipulate and reference them. One common example is a score in a video game. The score is stored in a variable, and whenever you defeat an enemy or pass a level, the variable’s value increases.

In Python, a variable is created with two components:

  • Name - you use the name to reference the variable later on
  • Value - this is the data that is being stored

Let’s first store a variable named number that has the value 5. Then, let’s use the print function we learned in the “Hello World!” article to see the variable’s value. Here is what it would look like:

number = 5
print(number)
> 5

Notice that unlike in the “Hello World!” article, we don’t have quotation marks around number in the print statement. This is because quotation marks indicate the computer should print the words between them. We don’t want to print the actual word “number,” we want the computer to print the value of the variable named number.

Here’s what would happen if we surrounded number with quotation marks:

number = 5
print("number")
> number

Math Using Variables

Now that we know how to create variables, we can manipulate and modify them.

For example, here is how we manipulate them using the four basic arithmetic operations (+, -, *, /):

a = 5
b = 5
# addition
print(a + b)
> 10
# subtraction
print(a - b)
> 0
# multiplication
print(a * b)
> 25
# division
print(a / b)
> 1.0

Note: Notice only the last number ends with .0 even though all four numbers technically end with .0. This is because integers and numbers with decimals are stored differently. Python originally stored a and b as integers but automatically converted them to a number with a decimal when the / division operator was used. Thus, when it was printed, it included its decimal despite not having a non-zero decimal.

We modify the value of an already-created variable with a line of code in the form “name of variable = new value of the variable.” If we wanted to change number from the previous example to have the value 10, this is what it would look like:

number = 5
number = 10
print(number)
> 10

In programming, the equals sign does not have the same purpose as in math. Instead, equal signs are used to set the variable on the LEFT equal to the value on the RIGHT. The value on the right is first calculated, then the variable on the left is assigned that value.

You can also use arithmetic when modifying a variable.

number = 5
number = 10 + 5
print(number)
> 15

You can even use the variable itself when modifying it.

number = 5
number = number + 2
print(number)
> 7

If you plan to use the variable itself when modifying it, you can use the shortcut below.

number = 5
number += 2
print(number)
> 7

This code does the same thing as the previous example but takes less space. You can also use this shortcut with any arithmetic operation by replacing + in the expression with the desired operation (-, *, /).

You can play with all the code we've used in this article on Trinket: