Variables

A variable is a named box that stores a value you can reuse and change. Learn to create them in real Python — running right here in your browser.

  • Create and use variables in Python
  • Explain what the = sign really does
  • Follow the rules for naming variables
  • Change a variable's value and predict the result

What is a variable?

A variable is a named box that stores a value. You give it a name, put a value inside, and use the name later wherever you need that value.

Definition: variable

A variable is a named storage location holding one value at a time. Assigning a new value overwrites the old one.

Python
name = "Aaquib"
age = 17
print(name)
print(age)

Here name and age are variables. The = sign assigns the value on the right to the name on the left — it does not mean "equals" the way it does in maths. Read age = 17 as "age gets 17".

Variables can change

Run the program below. Then change the code — set score to 100 — and run it again. Notice the old value is gone, replaced.

Try it: variablesEdit the code, then press Run.
Ready — press Run.

The computer executes instructions top to bottom. When it reaches score = 10, it throws away the old 0 and stores 10 instead. A variable holds one value at a time.

Naming rules

Python is picky about names. Break these rules and your program crashes:

  • Names can contain letters, digits and underscores: player_score, age2.
  • Names must start with a letter or underscore — never a digit. 2nd_place is illegal.
  • Names are case-sensitive: Score and score are different variables.
  • Choose meaningful names: total_price beats x every time.
Why names matter

You will read code far more often than you write it. total_price = 49.99 explains itself; x = 49.99 explains nothing. Good names are documentation that never goes out of date.

Variables in action

Variables become powerful when programs compute with them:

Python
apples = 12
friends = 4
each = apples / friends
print(each)   # 3.0 — each friend gets 3 apples

The program doesn't care that the numbers might change tomorrow. Update apples and friends, and each is recalculated automatically. That is the whole point of variables: write the logic once, reuse it with any values.

Practice

Create a variable called city holding your city, then print it.
Text values go in quotes: city = "..."
Python
city = "Bishkek"
print(city)
What is wrong with this line? 2nd_place = "Sara"
Check the naming rules.

Variable names cannot start with a digit. Rename it, e.g. second_place = "Sara".

After a = 3 then a = 7, what is the value of a? Why?
A variable holds one value at a time.

a is 7 — the second assignment overwrites the first.

Quick check

Variables — quick check

What does the = sign do in x = 5?

= is the assignment operator: it stores the right-hand value in the left-hand name.

Which is a valid variable name?

Names may start with a letter or underscore; hyphens and spaces are not allowed.

What does this print? a = 3 / a = 7 / print(a)

The second assignment overwrites the first, so a is 7 when printed.
Key takeaways
  • A variable is a named box storing one value; = assigns, it doesn't mean "equals".
  • Programs run top to bottom; reassigning a variable replaces its old value.
  • Names must start with a letter or underscore, and should be meaningful.
  • Variables let you write logic once and reuse it with any values.