You can already write print("Hello") and Python obeys. Now suppose you wanted to print Hello three times, in three separate lines. The slow way is to write print("Hello") three times. What if the word changes — to Hi — and you have ten copies?
I'd have to find every place I wrote Hello and change it. That's tedious and easy to miss one.
That's the problem variables fix. A variable is a name you give to a value. Once you store the value under a name, you write the name instead of the value, and one change at the top updates every reference:
greeting = "Hello"
print(greeting)
print(greeting)
print(greeting)What does the = mean? It looks like math, but math = means "is equal to".
In Python = means assignment: "take the value on the right, store it under the name on the left". Read it as "set". greeting = "Hello" is read "set greeting to Hello". The math meaning of equality is a different operator — == — which you'll meet later.
Can the name be anything?
Almost. Python rules: starts with a letter or underscore, then letters/digits/underscores. No spaces, no dashes, case-sensitive. greeting and Greeting are two different variables. Convention: lowercase with underscores between words — user_name, total_count.
And once I've stored "Hello" in greeting, can I change what's inside?
Just assign again. greeting = "Hi" overwrites the old value. The next print(greeting) shows Hi. The variable is a re-usable container, not a one-time label.
So the program reads top to bottom, and at any moment the variable holds whatever was last assigned to it.
That's the model. One name, one value at a time, updated by =.
A variable has three parts:
= (assignment) operatorage = 36From that line forward, age stands in for 36 until you reassign it.
Rules (Python won't run code that breaks these):
a–z, A–Z) or underscore (_)name and Name are different variablesif, for, def, etc. — Python uses these for itself)Conventions (Python will run anything legal, but readers will judge you):
user_name, total_countx is fine for tiny scripts; total_score is clearer in 50-line programs.print would hide Python's print function.x = 5
print(x) # 5
x = 10
print(x) # 10Each = replaces the previous value. Python doesn't keep history — once you assign, the old value is gone.
print(score) # NameError: name 'score' is not definedVariables only exist after you've assigned to them. Reading a name Python doesn't recognise is an error.
= is not equalityPython has two different operators that look similar:
| Operator | Meaning | Example |
|---|---|---|
= | assignment ("set") | x = 5 |
== | equality test ("is equal to") | x == 5 |
You'll meet == in the booleans lesson. For now, just remember: single = assigns, double == compares.
You can already write print("Hello") and Python obeys. Now suppose you wanted to print Hello three times, in three separate lines. The slow way is to write print("Hello") three times. What if the word changes — to Hi — and you have ten copies?
I'd have to find every place I wrote Hello and change it. That's tedious and easy to miss one.
That's the problem variables fix. A variable is a name you give to a value. Once you store the value under a name, you write the name instead of the value, and one change at the top updates every reference:
greeting = "Hello"
print(greeting)
print(greeting)
print(greeting)What does the = mean? It looks like math, but math = means "is equal to".
In Python = means assignment: "take the value on the right, store it under the name on the left". Read it as "set". greeting = "Hello" is read "set greeting to Hello". The math meaning of equality is a different operator — == — which you'll meet later.
Can the name be anything?
Almost. Python rules: starts with a letter or underscore, then letters/digits/underscores. No spaces, no dashes, case-sensitive. greeting and Greeting are two different variables. Convention: lowercase with underscores between words — user_name, total_count.
And once I've stored "Hello" in greeting, can I change what's inside?
Just assign again. greeting = "Hi" overwrites the old value. The next print(greeting) shows Hi. The variable is a re-usable container, not a one-time label.
So the program reads top to bottom, and at any moment the variable holds whatever was last assigned to it.
That's the model. One name, one value at a time, updated by =.
A variable has three parts:
= (assignment) operatorage = 36From that line forward, age stands in for 36 until you reassign it.
Rules (Python won't run code that breaks these):
a–z, A–Z) or underscore (_)name and Name are different variablesif, for, def, etc. — Python uses these for itself)Conventions (Python will run anything legal, but readers will judge you):
user_name, total_countx is fine for tiny scripts; total_score is clearer in 50-line programs.print would hide Python's print function.x = 5
print(x) # 5
x = 10
print(x) # 10Each = replaces the previous value. Python doesn't keep history — once you assign, the old value is gone.
print(score) # NameError: name 'score' is not definedVariables only exist after you've assigned to them. Reading a name Python doesn't recognise is an error.
= is not equalityPython has two different operators that look similar:
| Operator | Meaning | Example |
|---|---|---|
= | assignment ("set") | x = 5 |
== | equality test ("is equal to") | x == 5 |
You'll meet == in the booleans lesson. For now, just remember: single = assigns, double == compares.
Create a free account to get started. Paid plans unlock all tracks.