Overview
A stack is a Last-In First-Out structure; push adds to the top, pop removes from the top.
Analogy
A stack of plates: always add and remove from the top.
Step-by-step
- Python list as stack: push = lst.append(x), pop = lst.pop(), peek = lst[-1].
- Check empty: not lst or len(lst)==0.
- Stack overflow isn't an issue in Python (dynamic list).
Visual
push(1)→[1]; push(2)→[1,2]; pop()→2,[1]; peek→1
Common mistakes
- Popping from an empty stack raises IndexError; check before popping.
- Using insert(0,x) for push — that's a queue (deque) operation.
Practice questions
- Implement a stack-based RPN calculator.
- Check balanced parentheses using a stack.