Variables & Assignment
beginner10 min readLesson 5 of 169
Names that remember: assignment, naming, reassignment.
A variable is a name that refers to a value. Assignment uses = (read it as
"becomes", not "equals"):
age = 21
name = "Minh"
Variables let programs remember. Once assigned, use the name anywhere below:
price = 5
quantity = 3
total = price * quantity # 15
print(total)
Naming rules (and taste)
- Names may contain letters, digits, and underscores:
user_name,score2. - They cannot start with a digit or contain spaces.
- Case matters:
totalandTotalare different names. - Style: use
snake_caseโ lowercase with underscores. Future-you reads names as documentation.
Reassignment
Variables point to values; pointing can change:
score = 10
score = score + 5 # now 15
Python evaluates the right side first (10 + 5), then re-points the name.