Skip main navigation


Variables - Python
© Coventry University. CC BY-NC 4.0

Variables store information that can vary through manipulation.

We use variables in our everyday life. Checking your bank balance online, for example; the balance (value) is stored in a variable. The price of an item in a shop is stored in a variable, which is called upon when scanned. All the links on a website are also stored in a variable.

What is a variable?

In programming, a variable is used to store information that we can change and access on the fly. When we declare a new variable, it is stored in a specific spot in the computer’s memory. If we need to retrieve the value that we stored, we can just access it by using the variable’s name.

Variables are used for different reasons, depending on the situation. You can use them to ask the user for information and then store their response, to track the amount of times an event occurs, or even to trigger an event in your program depending on its value.

We will look at these examples in more detail over the next few steps.

How do we declare a variable?

Above we mentioned “declaring a variable”. Before we can declare it, however, we we need to do two things: provide a variable name and decide what information to store.

Variable naming

Naming variables is very important. Having an ambiguous variable name in your program can lead to confusion later on for both you and anyone else reading through it.

It should be short, accurate and descriptive.

We also need to follow some rules for naming variables otherwise we can cause errors in our program. These rules are that variable names must:

  • Not contain spaces
  • Only contain Letters (A-Z), numbers and underscores (_)
  • Start with either a letter or an underscore (variables cannot start with a number)

Here are some examples:

Good examples of variable names Bad examples of variable names
currentTime test (completely undescriptive)
userName1 5thUser (cannot start with a number)
_LifeCount TimeLeft! (cannot contain special characters such as )
Homepage_Link homepagelink (hard to read, try to use uppercase letters to help distinguish the start of a new word or separate words with underscores)

Variable declaration

Once we have decided on an appropriate variable name, we can actually declare our variable and store a value. The basic syntax for this is setting our variable name equal to our value:

VariableName = information

For example, let’s say you are a store owner and you currently have 12 apples and 13 oranges in stock. We can store this information in variables like so:

AppleCount = 12
OrangeCount = 13

Once declared, we can then retrieve that value again by calling the variable’s name. In the image below, you can see I am using the print() function to show the values stored in each of the variables.


Hopefully, now you understand the basics of declaring variables, why they are used and the general rules that bind them. To reinforce that, let’s do a small exercise on naming variables.

Your task

Think of five more examples where we would use variables in the real world and give them each an appropriate variable name. We’ve listed some examples below.
  • A user’s bank balance User_Balance
  • The price of a banana Banana_Price
  • The age of a user usersAge
© Coventry University. CC BY-NC 4.0
This article is from the free online

Get ready for a Masters in Data Science and AI

Created by
FutureLearn - Learning For Life

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education