Chapter 1 of 13
Course Progress0 of 13 completed (0%)

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

Your code

hello.py

Bytecode

compiled .pyc

Python VM

runs it step by step

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.

Hello, Python
1print("Hello, world!")
2print("I am learning Python")
3print(2 + 3)
Names
Objects in memory

Memory is empty

Step 0/3Program starts. Memory is empty.

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.

Names point to objects
1age = 21
2name = "Ada"
3print(name, "is", age)
4age = age + 1
5print(name, "is now", age)
Names
Objects in memory

Memory is empty

Step 0/5Program starts. Memory is empty.

Concept Check

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:

Two names, one list
1a = [1, 2, 3]
2b = a
3b.append(4)
4print(a)
5print(a is b)
Names
Objects in memory

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:

A real copy
1a = [1, 2, 3]
2b = a.copy()
3b.append(4)
4print(a)
5print(b)
6print(a is b)
Names
Objects in memory

Memory is empty

Step 0/6Program starts. Memory is empty.

Concept Check

After this code, what is nums?

nums = [5, 6]
more = nums
more.append(7)
Concept Check

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.

Python Memory Playground
Python

🎯 Not yet: Ali's list should stay ['milk', 'eggs'] while Sam's gets 'bread'. Watch the arrows below.

Your code in memory
1# Bug: changing the shopping list for Sam also changes it for Ali!
2ali = ["milk", "eggs"]
3sam = ali
4sam.append("bread")
5print("Ali:", ali)
6print("Sam:", sam)
Names
Objects in memory

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, or run real Python with full libraries in the Python Playground.

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 = y never 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

Enjoying this free lesson? Your support helps us build more free animated chapters.

Support Us