Register to get access to free programming courses with interactive exercises

Variables Python Basics

In this lesson, we will learn how to work with variables in Python, including common mistakes inexperienced developers make and how to fix them.

How to create variables

Suppose we want to type the phrase Father! twice on the screen. We can solve this problem by doing the following:

print('Father!')
print('Father!')

In the simplest case, this is what you should do. But if the phrase Father! is used more than twice, and even more in different parts of the program, then you will repeat it everywhere — it is inconvenient. Then you will have even more problems when it turns out that you need to change the phrase. It is a common scenario in development. We will look where this phrase appears and make the necessary changes repeatedly.

There is one other way to do it. If you do not want to copy the expression, you should create a variable with it:

# greeting
greeting = 'Father!'
print(greeting)
print(greeting)
# => Father!
# => Father!

Let us see how it works:

Variable definition

In the line greeting = 'Father!', we take a variable named greeting and assign it the value 'Father!'. The variable refers to the data written to it. This way, we can use the data repeatedly and not be duplicated constantly.

Once you have created the variable, you can use it. You put it in the places where we originally had our phrase written out in full. When the code runs, the interpreter reaches the print(greeting) line, substitutes the contents of the variable, and then executes the code.

We can use any set of valid characters for the variable name, including letters of the English alphabet, numbers, and the _ sign. Note that you cannot place a digit at the beginning of a name. Variable names are case-sensitive. For example, hello and HELLO are two names for two different variables. Never forget about the case. It is essential in Python.

The number of variables you can create is unlimited. Large programs contain tens or hundreds of thousands of variable names. It is what two variables look like inside one program:

greeting1 = 'Father!'
print(greeting1)
print(greeting1)

greeting2 = 'Mother!'
print(greeting2)
print(greeting2)

To make the program easy to read, it is customary among programmers to create variables as close as possible to where we use them. Now we have to figure out how to change them.

How to change variables

The word variable suggests that we cannot change it. And indeed, the values of variables can change over time within the program.

For example:

# It is a greeting
greeting = 'Father!'
print(greeting)  # => Father!

greeting = 'Mother!'
print(greeting)  # => Mother!

The name remained the same, but there were different data inside. Note that variables in Python require no special declaration. Instead, a variable is declared when we use it in a program for the first time.

Variables are a powerful yet risky thing. You can't be sure right away what'll be inside it - first, you have to analyze the code that comes before the variable. It is what developers do during debugging when they try to figure out why the program doesn't work as intended.

How to avoid mistakes with variables

The order of instructions in code with variables is of great importance. That is why we must define the variable before we use it for the first time. Below is an example of a mistake often made by beginners:

print(greeting)
greeting = 'Father!'

Running the program above ends with the NameError: name 'greeting' is not defined — this is a reference error. It means the code uses a name (identifier) we haven't defined yet. It is stated in the error text itself: 'greeting' is not defined. In addition to incorrect ordering, you can also come across simple typos in the variable name. It happens both when we use the variable and when we declare it.

You can reduce the number of such errors if you use a properly configured editor. It warns of possible problems and highlights variables used without being declared.

Now we got the variables figured out and can move on to data that never change.

How constants work

Some data never change - for example, mathematical constants. Take π as an example. It is always 3.14. Python uses constants to refer to this kind of data:

PI = 3.14
print(PI)  # => 3.14

A constant works the same way as a variable. The only difference is that constants are usually named with capital letters and with _ as a separator between words. A constant, like a variable, can be used in any expression.

Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending this form, you agree to our Personal Policy and Service Conditions

Our graduates work in companies:

Bookmate
Health Samurai
Dualboot
ABBYY