Last tray on, first tray off
Stacks as Tray Piles
The analogy
Clean trays stack up in the cafeteria. You put a tray on top, and the next person takes the top one — the tray at the bottom has been there since breakfast. You only ever touch the top. That is a stack.
Visualizer
The tray pile
step 1 / 14bottom
An empty tray pile. There is exactly one place you can reach: the top.empty
stack = [] stack.append(4) # push stack.append(9) stack.append(2) stack.pop() # 2 — last in, first out stack[-1] # 9 — peek # a plain list IS the idiomatic Python stack
Check yourself
Push 1, 2, 3 then pop. What comes out?