1.LIFO principle
A stack is Last-In-First-Out: the most recently added item is always the first one removed. Everything below it stays buried until it is.
Key features
- push adds to the top; pop removes from the top — no other position is ever touched directly
- The only item directly reachable is the top — reaching anything below it means popping everything above it first
- Contrast with a queue (FIFO): a stack always serves the newest item first, a queue always serves the oldest
Command
stack = []
stack.append('a')
stack.append('b')
stack.append('c')
print(stack.pop()) # 'c' — the most recently pushed item, out first
print(stack) # ['a', 'b'] — 'a' is still buried under 'b'LIFO means the last thing pushed is the first thing popped — picture a stack of plates, not a line of people.