Module 01 · Lesson 03
Variables & Dynamic Typing
Store values under names, change them as the program runs, update them with +=, swap two at once, and mark values that should never change as constants.
- About 8 minutes
- Beginner friendly
- 12-question quiz at the end
What you will learn
- Name a value
- Change and swap
- Watch names update
- Constants in capitals
Give a value a name
A variable is a name you stick on a value, so your program can find it again later.
Name it
score = 0sticks the namescoreon the value 0. The=means "store", not "is equal to".Change it
Write
score = 10later and the name moves to the new value. The old one is simply forgotten.Name rules
Letters, digits and underscores only, never a digit first.
Scoreandscoreare two different names.CONSTANTS
A name in capitals, like
MAX_LIVES = 3, tells people "please do not change this".
Every app remembers with names
Behind each number or word that changes on a screen sits a variable.
- Game scoresA game keeps
score,livesandleveland changes them every time something happens. - Music playersThe song playing now and the volume are two variables that update as you tap.
- Weather appsToday's temperature is stored under a name and shown in big numbers on the home screen.
- Step countersA watch adds 1 to a
stepsvariable every time you take a step.
Right side first, then store
Python reads an assignment from the right. Tap each part to see its job.
Part 1 of 5
new name:The name that gets the answer. It is filled in last, after the right side is worked out.
Make names and change them
Pick an example, change it, then press Run. Say what you expect to see before you press.
Example 1 of 6Name things
Each line stores a value under a name. print() then shows what the names point to.
Your turn: Add a town variable and print one more line about Sam's town.
name = "Sam" age = 12 pet = "dog" print(name, "is", age) print(name, "has a", pet)
Press Run to see what it prints.Watch the names change
Press Step to run one line at a time. Keep an eye on the names box: which values change, and which stay put?
Ready. Press Step to run the first line.
- coins = 10
- coins = coins + 5
- bonus = coins * 2
- coins = 0
- print("coins:", coins)
- print("bonus:", bonus)
Output so far
Nothing printed yet.
What Python does
Python reads the program from the top. Nothing has run yet.
Names
None yet.
No type label needed
Storing one score in Python and in two other languages.
Language 1 of 2Python vs Java
score = 0
score = "zero"No keyword and no type. The same name can later hold text instead of a number: Python checks the value, not the name.
Quick guesses
Guess first, then read why. A wrong guess is where the learning happens. Nothing here is scored.
Question 1 of 6
1.What does this print?
a = 5
b = a
a = 9
print(b)0 of 6 answered
Common name misconceptions
Each card is something beginners often believe. Tap it to see what Python really does.
Misconception 1 of 4
Notes
Everything above, in words. Open it when you want the full explanation.
Read the notesNotesOpenClose
Syntax
The general shape of the statements in this lesson.
name = value # create or replace a variable count = count + 1 # right side first, then store count += 1 # the same, shorter (-=, *=, /= too) x, y = 1, 2 # several names at once x, y = y, x # swap MAX_SPEED = 120 # constant: UPPER_CASE by convention
Press Run to see what it prints.Worked examples
Read the problem, run the code, then read why it works.
Example 1 of 2
A savings tracker
Start with 20 in savings, add 15 for each of three weeks with +=, spend 12, and print the balance after each step.
WEEKLY_SAVING = 15
savings = 20
savings += WEEKLY_SAVING
savings += WEEKLY_SAVING
savings += WEEKLY_SAVING
print("After three weeks:", savings)
savings -= 12
print("After the cinema:", savings)Press Run to see what it prints.Predict first
How it works(open after your guess)
Common pitfalls
Mistakes people make with this topic, and what to write instead.
Pitfall 1 of 3
Starting a name with a digit
Spot the bug
Can you see what is wrong? Run it to watch it go wrong, then open the fix.
2nd_place = "Lee"
Press Run to see what it prints.Show what goes wrong and the fixWhat goes wrong and the fix
In the real world
Where this shows up in software people use every day.
- Events
Counting visitors at a door
A volunteer at a school fair counts visitors in and out with a simple program, so they always know how many people are inside.
Why Python fits
The capacity never changes, so it is a constant. The count changes with every arrival and departure, so it is an ordinary variable updated with
+=and-=.PythonCAPACITY = 120 inside = 0 inside += 45 inside += 30 inside -= 12 print("Inside now:", inside) print("Space left:", CAPACITY - inside)OutputPress Run to see what it prints.
Key takeaways
Tick each one you could explain to a friend.
0 of 5
Common questions
Guess the answer first, then open it.
Is a constant really locked?
Can a variable change from a number to text?
What is the difference between = and ==?
Your notes
Sign in to keep private notes alongside this lesson.
Where to go next
The quiz and the Lab live on their own pages, so this lesson stays about learning.
Finished this lesson?
Create an ID or sign in to save your progress across devices.
Check what you learned
12 questions, with an explanation after every answer. A verified attempt counts towards opening the module Lab.
Keep going in the Lab
7 tasks on this lesson, from writing code to tracing it by hand. Practice only, no XP.
Spaced review
Questions you miss in a quiz come back on a spaced schedule, so what you learned here sticks.
This lesson at a glance
The 6 lines of code this lesson uses, each with a short note. Keep it open while you do the quiz and the Lab.
What else you can search for and explore
Curious to go further? Each card opens a web search in a new tab.
References
Look things up here whenever you need the exact details.