Python Variables: Assignment, Reassignment, and Naming

Python 3.14 · ✓ verified by execution on 2026-07-19

A variable is a name that refers to a value, so you can store something now and use it later. You create one with = — read it as “store this value in this name.” Run this, then change "Ada" to your own name:

python
name = "Ada"
print(name)
Output
Ada

The name name now refers to the string "Ada", so print(name) shows it. Variables can hold any value — a number, for example:

python
age = 36
print(age)
Output
36

And you can use several variables together:

python
first = "Ada"
last = "Lovelace"
print(first, last)
Output
Ada Lovelace

= means “store”, not “equals”

This trips up almost everyone at first. In maths, = states a fact. In Python, = gives an instruction: take the value on the right and store it in the name on the left. It always flows right-to-left. That’s why a name can change — which is the next idea.

Reassignment: a variable can change

Assign a new value to the same name and it simply replaces the old one. The name always refers to whatever you stored in it most recently:

python
status = "loading"
print(status)
status = "ready"
print(status)
Output
loading
ready

The best way to understand this is to watch it happen. Press Visualize and step through — notice how the value next to each name updates as the program runs:

python · visualize
player = "Sam"
score = 0
score = 50
score = 120
print(player, score)

player stays "Sam" the whole time, while score is rebound three times until it finally refers to 120.

A variable can even change type

Because Python figures out types as it runs, reassigning a variable can change its type too. Remember type() from the last lesson? Watch it change:

python
data = 42
print(type(data))
data = "forty-two"
print(type(data))
Output
<class 'int'>
<class 'str'>

Naming rules

A variable name must:

The common style in Python is snake_case — lowercase words joined by underscores:

python
user_score = 100
print(user_score)
Output
100

One error you’ll meet: NameError

If you use a name you never assigned, Python doesn’t know it and stops:

python
print(total)
This example raises an error (on purpose)
NameError

Read it as “I’ve never heard of total.” The fix is to assign it first: total = 0.

Edge cases worth remembering

Check yourself

What does the = symbol do in Python?

Reveal answer

Stores a value in a name (assignment) — = means "store this value in this name." It is an instruction, not a question. Checking equality is a different operator, ==, which you'll meet later.

After running score = 10 and then score = 25, what does print(score) show?

Reveal answer

25 — Reassignment replaces the value. The name score refers to whatever was assigned to it most recently — 25.

Which of these is a valid variable name?

Reveal answer

user_score — Names must start with a letter or underscore and contain no spaces. user_score is fine; 2nd_score starts with a digit, and "user score" has a space.

Are Score and score the same variable?

Reveal answer

No — names are case-sensitive, so they are different — Python names are case-sensitive. Score and score are two completely separate variables.

Challenges

Challenge 1 +10 XP

Create a variable called planet that holds the text "Earth", then print it.

python
  • Test 1 — expects "Earth"
Need a hint? (−25% XP)

Use planet = "Earth" on one line, then print(planet) on the next.

Show solution (0 XP)
planet = "Earth"
print(planet)

Challenge 2 +15 XP

Create a variable count set to 3. Then reassign it to 7. Then print count (it should show 7).

python
  • Test 1 — expects "7"
Need a hint? (−25% XP)

Assign 3 first, then assign 7 to the same name — the newer value wins.

Show solution (0 XP)
count = 3
count = 7
print(count)