4. Variables

What Are Variables?

Learning about variables is the first major step in programming. Every programming language in the world has variables.

A variable is a name that you can create in your code that holds a value. Variables also have a type depending on the value we store in the variable.

Without variables, a program wouldn't be very useful. Creating software usually involves working with data. We store data in variables and perform calculations or make decisions on what the variables contain.

A real world example would be a piece of code that calculates the total of a shopping basket. Each item in the basket has a price. The price of each item will be stored in a variable. We can add the prices of the basket items contained in the variables together and create a new variable that holds the total price. We could then use that total variable to show the customer on screen or perhaps print it out on a receipt.

We are going to look at a few of the most commonly used types of variables and how to use them.

Integer Variables

The first type we will look at is what we call the 'Integer' type. An integer is a whole number that can be negative, positive, or 0. Integers can not be fractional numbers such as 3.14 or 0.09.

Let's look at some code that creates an integer variable. We will also use the print statement to show the value of the variable:

x = 1

Line 1 is where we are creating the variable, or better known as 'defining' the variable. Its name is 'x'. The equals sign after the name is what we call an 'assignment operator'.

The assignment operator '=' tells Python that we want to set the value of the variable with the number directly after the operator. In our code, the number 1 is after the operator, so Python sets the value of the variable to 1. If you run this code, you will see the nuber 1 as output in the terminal which is produced from our line 2. Notice how we put the variable name in between the brackets of the print statement.

Variables get their name from the fact that once we create them, we can change them whenever we like. You can keep using the name of the variable and the assignment operator '=' to keep changing the value. The code below demonstrates this:

x = 1

x = -5

x = 24

Try running this code and you should see the numbers 1, -5, and 24 output in the terminal. Each time we set the variable on lines 1, 4, and 7, we print the value using the print statement directly after.

Arithmetic Operators

Now that we know how to create a variable, lets take a look at some more Python operators. When assigning a value to a variable, you can use multiple numbers with various 'Arithmetic Operators', much like how you would operate a calculator.

The first operator we will look at is the addition operator '+'. Here's some code showing how we can use this with multiple numbers:

x = 1 + 2

x = 2 + 5 + 10

x = 1 + 10 + 100 + 5

Let's talk for a moment how Python considers this code. When we use multiple values and multiple operators to assign a variable, Python must first 'evaluate' the code and come up with a value before assigning it to the 'x' variable. If we consider this on line 1, Python will first calculate the answer of 1 + 2, which is 3. It then uses this value of 3 and assigns it to 'x'.

Now that you have seen the addition operator '+', lets move quickly through some other commonly used operators when working with numbers.

Subtraction '-':

x = 10 - 5

Multiplication '*':

x = 2 * 10

Division '/':

x = 10 / 2

The next set of operators are not so common but I need to make you aware of them in case you stumble upon them when reading Python code. Feel free to Google the next few operators if you're curious to know more of how they work.

Modulus '%' (meaning, the remainder after division):

x = 12 % 5

Exponentiation '**' (multiply a number with itself for a number amount of times):

x = 10 ** 5

Floor Division '//' (divide two numbers and round down to the nearest whole number):

x = 7 // 3

Assignment Operators

Earlier in this section, we learned how to define variables using the assignment '=' operator. Once a variable is defined, and has a value, we can use arithmetic assignment operators. Consider the following code:

x = 10
x += 5

Line 2 shows a new operator '+=' which means add the number on the right to the value of 'x' and assign the result to 'x'. The output in the terminal should show 15.

For quickness, the following code shows all of the assignment operators. As we've already seen the arithmetic operators above, most of this should make sense now:

x = 10
x += 5
x -= 2
x *= 10
x /= 2
x %= 4
x **= 5
x //= 3

If you're not comfortable with the above, try playing around with these various assignment operators using the print statement to check the value of the 'x' variable. Don't worry too much about the last 3 in the list, as these aren't so common.

Variables Replacing Values

So far, we've seen how to define a variable and the various ways we can change its value. All the examples we've used have been with whole numbers. Instead of using whole numbers, we can use variables in place of the numbers. When we do this, Python will use the value contained in the variable name when evaluating the code.

Here's a simple example of variables working together:

slices_per_cake = 8
number_of_cakes = 10

total_slices = slices_per_cake * number_of_cakes


Run this code, and you might have already guessed that the output is 80. We're using the multiplication operator on line 4 to multiply two variable values together, and assign it to a new variable to see how many total slices of cake we have (mmm, cake...).

Note: You can use a mix of values and variable names in statements, try it!

Variable Naming

In the last code example, we have much larger variable names than the 'x' that was used in earlier examples. You may also have noticed how we have used underscores '_' to separate the words in the variable names. Using underscores in a variable name that contains multiple words is a nice way to separate words and help keep the code easy to read.

Every programming language has its own preferred way of naming things. For example, there are other languages that don't suggest using an underscore in names, so instead of 'total_slices', it might suggest the style of 'totalSlices' instead. It's usually best to follow the known convention of the language you're using so that other developers who write and share code for that language, share a consistent look and feel. The further we learn through this course, the more you will get to see how to write Python code in its convention, so don't worry about this for now.

One final but important note about variable names, is that they are case sensitive. This means you can have the same names, but different casing, and they are considered to be different variables. Here's an example:

order_total = 100
Order_total = 200


There might come a day where you accidentally misspell a variable name, causing your program to not have the right value in one of your variables. We call those kinds of problems 'bugs'. There will be a section much later on that will show you how to become an effective bug squasher.

Right, back to variable types. We'll cover one more variable type before closing off this section.

Floating Point Variables

We've seen integer variables for holding whole numbers, but what about decimal numbers? Meet, the 'Floating Point' variable type. There is a lot of theory around why this type is called floating point, which is not necessary to go in to at this time.

To define a variable as a floating point type, use the decimal point in the number value. Here's an example:

temperature = 68.25

The presence of the decimal point in the number assigned to the variable tells Python that this variable is a floating point type. When performing arithmetic on this variable, the variable value will maintain the decimal point.

You can use all of the arithmetic operators on floating point variables exactly as you did with integer variables. For example:

temperature = 68.25 + 7.02
temperature += 5

If you assign a whole number to a floating point variable, the type of the variable will change from floating point to integer. The reverse is also true. Assigning a decimal point value after defining a variable as an integer will change its type to floating point.

To demonstrate this, we're going to learn how to output what the type name of a variable is, to the terminal. The following code example shows how a variable can change type as we keep assigning to it:

temperature = 68.25 + 7.02

temperature = 70

temperature = 72.5

When you run this code, you will see in the terminal the 3 print statements from lines 2, 5, and 8. The output terminal text "<class 'float'>" indicates the type of the variable in the print statement is a float, and "<class 'int'>" indicates the type is an integer.

What We Learned

Phew, that was quite a lot of details to take in. I suggest you make notes to help absorb the information and do a lot of your own playing around with the code examples above. Here's a summary of what we went through: