Python Mastery · Chapter 1 · ~15 min · Beginner
Your First Program & How Python Remembers Things
By the end you'll write your first program, see exactly what Python does with every line, and fix the bug that trips up more beginners (and job interviews) than any other.
- Run code with print()
- Know Python reads top to bottom
- See variables as labels, not boxes
- Spot and fix the shared-list bug
1A program is a list of instructions
A program is just a recipe. Python reads it one line at a time, from top to bottom, and does what each line says. The simplest instruction is print() — it shows something on the screen.
Press Play and watch the arrow move down the code while the output appears.
1print("Hello, world!")2print("I am learning Python")3print(2 + 3)
Memory is empty
Step 0/3Program starts. Memory is empty.
"Hello"). Numbers don't. print(2 + 3) shows 5, but print("2 + 3") shows the text 2 + 3.2Variables are name tags, not boxes
Many books say a variable is a "box" that holds a value. That picture breaks later. The real picture is simpler: Python makes an object in memory (the value), then sticks a name tag on it.
Step through this and watch each name point to its object. Every object gets an ID number — its address in memory.
1age = 212name = "Ada"3print(name, "is", age)4age = age + 15print(name, "is now", age)
Memory is empty
Step 0/5Program starts. Memory is empty.
22 and moves the age tag onto it. The old 21 is thrown away when nothing points to it.What does this print?
x = 10 y = x x = 20 print(y)
3The shared-list trap
Numbers and text can never change once made (they're immutable). Lists can be changed in place (they're mutable). Combine that with name tags and you get Python's most famous surprise:
1a = [1, 2, 3]2b = a3b.append(4)4print(a)5print(a is b)
Memory is empty
Step 0/5Program starts. Memory is empty.
b = a doesn't copy anything — it puts a second tag on the same list. So adding to b also changes what a sees. is asks “are these the same object?”
Now compare with a real copy — watch a second list object appear:
1a = [1, 2, 3]2b = a.copy()3b.append(4)4print(a)5print(b)6print(a is b)
Memory is empty
Step 0/6Program starts. Memory is empty.
After this code, what is nums?
nums = [5, 6] more = nums more.append(7)
Which line makes an independent list?
4Hands-on: fix the shopping-list bug
Ali and Sam share a shopping list by accident. Edit the code so Sam gets "bread" but Ali's list stays ["milk", "eggs"]. Press Run & visualise — the diagram shows whether you really have two lists.
🎯 Not yet: Ali's list should stay ['milk', 'eggs'] while Sam's gets 'bread'. Watch the arrows below.
1# Bug: changing the shopping list for Sam also changes it for Ali!2ali = ["milk", "eggs"]3sam = ali4sam.append("bread")5print("Ali:", ali)6print("Sam:", sam)
Memory is empty
Step 0/5Program starts. Memory is empty.
Need a hint?
Line 3 only copies the label. Ask Python for a new list instead: sam = ali.copy() (or list(ali), or ali[:]).
Want to keep your script? Paste it into Online Notepad++ and save it as shopping.py.
5Recap
- Python runs your code top to bottom, one line at a time.
- A variable is a name tag pointing to an object in memory.
x = ynever copies — it adds another tag to the same object.- Numbers and text are immutable; lists are mutable. Use
.copy()when you need your own list.
Solve the exercise above to complete the chapter, or