- 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.
A variable is a named storage location holding one value at a time. Assigning a new value overwrites the old one.
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.
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_placeis illegal. - Names are case-sensitive:
Scoreandscoreare different variables. - Choose meaningful names:
total_pricebeatsxevery time.
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:
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
city = "Bishkek" print(city)
Variable names cannot start with a digit. Rename it, e.g. second_place = "Sara".
a is 7 — the second assignment overwrites the first.
Quick check
What does the = sign do in x = 5?
Which is a valid variable name?
What does this print? a = 3 / a = 7 / print(a)
- 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.