Python Variable: Storing Information for Later Use

What is a Python variable?

Now that we can use Python as a calculator by leveraging the REPL, wouldn’t it be nice to store the results of those calculations? For this, we can use a Python variable. Let’s start by defining more formally what a variable is:

A variable is used to store information that can be referenced later on.

Creating a Python variable

We can create a Python variable called result in the REPL. But before we do so, we’ll try and see if Python already knows what result is:

>>> result
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'result' is not defined

This is the way Python lets you know about errors. Ignore the first two lines and focus on the actual error instead. Python reports: name 'result' is not defined. Python errors tend to be very helpful if you know where to look. That’s why I wanted to show you one. Eventually, you’ll need to write code on your own, and running into errors is, unfortunately, a big part of the job. Being able to decipher errors will be a useful skill!

Now let’s define the name result as a variable and try this again:

>>> result = 3 * 5
>>> result

Step by step this is what happens:

  • Python sees a so-called assignment: we assign the result of 3 * 5 to the variable called result. Assignments are done with the ‘=’ character, which is conveniently called ‘is’. So we just told Python: result is 3 times 5.
  • Next, we type result.
  • Python does not recognize this as a command, so it tries to see if, perhaps, there’s a variable with this name. There is, and we assigned 15 to it. Hence this line evaluates to the number 15, which is printed on the screen.

We picked the general name result here, but you can choose any name you deem appropriate. As a general rule, always pick a variable name that best describes its contents. This practice makes your code more readable and easy to understand. If we were calculating the total price of a shopping card here, for example, a good name would be shopping_card_price.

Using variables in expressions

Python variables are a crucial part of the language because you can use them in other expressions too:

>>> 4 * result
>>> result - result
>>> _

I used the word expressions without explaining what an expression is, let’s fix that:

An expression is anything that Python can evaluate to a value

These are all valid expressions because Python can evaluate them to a value:

# Evaluates to 9:
3 * 3

# Evaluates to 19 if the result is 15:
result + 4

# Simply evaluates to 2:

Variable type

A value assigned to a variable does not have to be a number, by the way. Python has several data types besides numbers, and they can all be the result (the value) of an expression. One such data type is the Python string, which is, uncoincidentally, the topic of the next article in the tutorial!

Python has a built-in function called type(), which we can use to determine a variable or expression type. Here are some examples:

>>> my_number = 4
>>> type(my_number)
<class 'int'>
>>> my_string = 'Hello'
>>> type(my_string)
<class 'str'>

First, we created a variable with the value 4. When asked, Python tells us this variable is of class int, which is short for integer. Follow the link for a more thorough explanation of integers, if you like.

Next, we create a string. And when asked, Python indeed tells us that it is of class str, short for string. It’s now time to learn more about strings.

Please help us and share this article

About the author

Erik is the owner of Python Land and the author of many of the articles and tutorials on this website. He's been working as a professional software developer for 25 years, and he holds a Master of Science degree in computer science. His favorite language of choice: Python!