Free Python course. Sign Up for tracking progress →

Python: What is a variable

Imagine that we need to print the phrase Father! on the screen twice. This can be done like so

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

In the simplest case, that's what you should do. But if the phrase Father! is used more than twice, or even in different parts of the program, you have to repeat it everywhere; it's a little inconvenient. The problems with this approach will begin when you need to change the phrase, and this happens quite often. We'll have to find all the places this phrase is located and make the required changes lots of times.

There is one other way to do it. In order not to copy the expression, you just need to create a variable with it.

# greeting - translates as greeting
greeting = 'Father!'
print(greeting)
print(greeting)
# => Father!
# => Father!

In the line greeting = 'Father!' we take a variable named greeting and assign it the value 'Father!' The variable points to the data that was written to it. In this way, the data can be used repeatedly and not be duplicated constantly.

Once you've 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 line print(greeting), substitutes the contents of the variable, and then executes the code.

Any set of valid characters can be used for the variable name, which includes letters of the English alphabet, numbers and the _ sign. Note that you can't place a digit at the beginning of a name. Variable names are case-sensitive, i.e., the name hello and the name HELLO are two different names for two different variables. Case is important in Python, never forget it.

The number of variables you can create is unlimited. Large programs contain tens or hundreds of thousands of variable names. This 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's customary among programmers to create variables as close as possible to where they are used. Now we have to figure out how to change them.

Instructions

Create a variable named motto with the contents What Is Dead May Never Die!. Print its contents.

The exercise doesn't pass checking. What to do? 😶

If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:

  • Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
In my environment the code works, but not here 🤨

Tests are designed so that they test the solution in different ways and against different data. Often the solution works with one kind of input data but doesn't work with others. Check the «Tests» tab to figure this out, you can find hints at the error output.

My code is different from the teacher's one 🤔

It's fine. 🙆 One task in programming can be solved in many different ways. If your code passed all tests, it complies with the task conditions.

In some rare cases, the solution may be adjusted to the tests, but this can be seen immediately.

I've read the lessons but nothing is clear 🙄

It's hard to make educational materials that will suit everyone. We do our best but there is always something to improve. If you see a material that is not clear to you, describe the problem in “Discussions”. It will be great if you'll write unclear points in the question form. Usually, we need a few days for corrections.

By the way, you can participate in courses improvement. There is a link below to the lessons course code which you can edit right in your browser.

Tips

Definitions

  • Variable a way to save information and name it for later use in code.


If you got stuck and don't know what to do, you can ask a question in our community