Learning Goals
3 minBy the end of this lesson you can:
- Create a variable using
=and read it back withprint(). - Update a variable so it holds a brand-new value.
- Choose clear, snake_case names that follow Python's rules.
Warm-Up
5 minLast lesson we shaped print() with sep, end, comments, and \n.
Quick puzzle. Imagine a five-line poem that mentions your name on every line.
print("Aisyah woke at 7am.") print("Aisyah had roti canai for breakfast.") print("Aisyah walked to school.") print("Aisyah learned Python.") print("Aisyah slept happily.")
Tomorrow the same poem must say Wei Jie instead. How many lines do you need to change?
Five lines is too many. Today we learn how to change one line so all five update at once.
New Concept · Variables — Boxes with Labels
12 minPart 1 · The labelled-box analogy
A variable is a labelled box that holds one value. We make a box with =.
Left of = is the label. Right of = is what we put inside.
name = "Aisyah" age = 12
To read the box, just use its label:
print(name) print("I am", age, "years old.")
Part 2 · The = sign is not "equals"
In maths, = means "are these the same?". In Python it means something else.
It tells Python to put the value on the right into the box on the left.
Python works out the right side first, then stores the answer in the box.
Part 3 · Re-assigning — changing what's inside
The box only holds one value at a time. We can swap it for a new one later.
score = 0 print(score) score = 5 print(score) score = score + 10 print(score)
The last line looks odd. Read it as: take the current score, add 10, put the result back in the box.
Part 4 · Naming rules (must follow)
- Must start with a letter or an underscore
_. - May contain letters, digits, and underscores.
- No spaces. No hyphens. No other symbols.
- Cannot be a Python keyword (like
if,for,print). - Python is case-sensitive —
Scoreandscoreare different boxes.
Part 5 · Naming style (please follow)
- Use snake_case — lowercase, words joined by underscores:
player_score. - Be descriptive.
player_scorebeatsxors. - Single letters are only fine in tiny counters — we will meet those soon.
Quotes still matter. name = "Aisyah" stores a word. name = Aisyah tries to read a box called Aisyah — and crashes if it doesn't exist.
Worked Example · A Birthday Card
12 minWe will build a tiny birthday card. The data lives in variables at the top — change one variable and the whole card updates.
Code
# birthday_card.py — a small birthday card # 1. Card data — change these to make a brand-new card recipient = "Aiman" age_today = 13 sender = "Faiz" # 2. Build the card print("============================") print(" Happy Birthday!") print("============================") print("To", recipient, end="") print(", who is now", age_today, "today.") print("Have a great year ahead.") print("With love,", sender) print("============================")
Run it. You should see:
Output
============================
Happy Birthday!
============================
To Aiman, who is now 13 today.
Have a great year ahead.
With love, Faiz
============================Now change recipient = "Aiman" to recipient = "Mei Ling". Run again. The card updates everywhere — that is the magic of variables.
Re-assignment in action
Variables shine when something changes over time, like a game score:
score = 0 print("Kick-off:", score) # A goal! score = score + 1 print("First goal:", score) # Another goal! score = score + 1 print("Second goal:", score)
Try It Yourself
13 minType each task into your editor. Run it. Fix any typos before moving on.
Create three variables: my_name, my_age, and my_town. Then print a friendly two-line introduction using all three.
Hint
Words go in quotes; the age is just a number. Use a comma inside print() to mix them.
Make a variable points = 0. Print it. Re-assign to 100. Print it. Then re-assign to points + 50. Print it once more.
Hint
You will write the line points = points + 50. Read it as "new points = old points + 50".
Each line below tries to make a variable. Three are broken. Find them, then write fixed versions in your editor.
1st_place = "Aiman" my name = "Mei Ling" total-score = 50 favourite_meal = "nasi lemak" _secret = "shhh" PLAYER_2 = "Priya"
Hint
Names must start with a letter or underscore, and they cannot contain spaces or hyphens.
Mini-Challenge · Build an ID Card
8 minBuild a tidy "ID card" for yourself. The card should pull all its details from variables at the top of the file.
Your file must include:
- At least five variables: full name, age, school year, favourite subject, dream job.
- One calculation in a sixth variable, for example
years_until_18 = 18 - age. - A short comment above each variable explaining what it stores.
- An output card built with
print()— use a triple-quoted string orsep="\n", your choice.
Stretch goal. After printing the card, re-assign dream_job to a different value, then print "Update — new dream job:" with the new value.
Show one possible solution
# id_card.py — a small ID card built from variables # Personal details full_name = "Nur Aisyah Razak" age = 12 school_year = "Form 1" favourite_subject = "art" dream_job = "video-game designer" # Calculated detail years_until_18 = 18 - age # Output print("===========================") print(" Advaslearning ID Card") print("===========================") print("Name:\t", full_name) print("Age:\t", age) print("Year:\t", school_year) print("Loves:\t", favourite_subject) print("Dream:\t", dream_job) print("Until 18:", years_until_18, "years") print("===========================") # Update the dream job dream_job = "AI researcher" print("Update — new dream job:", dream_job)
Recap
3 minWe gave Python a memory. Variables let one change at the top update the whole program below.
Vocabulary Card
- variable
- A labelled box that stores one value at a time.
- assignment (=)
- Puts the value on the right into the variable on the left.
- re-assignment
- Putting a new value into an existing variable.
- snake_case
- Naming style used in Python — all lowercase, words joined by underscores.
Homework
4 minCreate a new file called me.py that introduces you to the class.
Rules:
- At least five variables describing you (any details you like).
- A short intro printed using all five variables.
- One re-assignment near the end — for example, change
moodfrom"happy"to"sleepy"and print the update. - One comment at the top with the file's purpose.
Save the file and bring it to the next lesson — we will do show-and-tell!
Sample · me.py
# me.py — a short introduction built from variables # Who I am name = "Aisyah" age = 12 city = "Kuala Lumpur" favourite_food = "nasi lemak" mood = "happy" # Print intro print("Hi! My name is", name) print("I am", age, "years old.") print("I live in", city) print("My favourite food is", favourite_food) print("Right now I feel", mood) # After a long Python lesson... mood = "tired but proud" print("Update — now I feel", mood)
Yours can be totally different — different name, different details, different mood. As long as you have five variables, one intro, and one re-assignment, you've nailed it.