School Revise · Class 11 Computer Science · Chapter 3
Code 083, Class 12. A stack is a simple data structure where the last item added is the first one out. Here we build a stack using a Python list.
|
|
A stack stores items so that the last one added is the first one removed. This is called LIFO, last in first out, like a pile of plates.
A Python list works as a stack. append() pushes an item onto the top, and pop() removes and returns the top item.
|
stack = [] stack.append(10) stack.append(20) print(stack.pop()) Output: 20 |
Peek looks at the top item without removing it, using stack[-1]. The stack is empty when its length is zero.
Stacks are used for the undo feature, for reversing items, and by the computer to remember function calls. Their LIFO order makes all these easy.
Explore the idea by tapping. The interactive opens right here in the lesson.
Write and run real Python right here in the lesson, then work through the practice problems with answers.
Type each one into the code lab above, then open the card to see the worked solution and its output.
SOLUTION
OUTPUT
SOLUTION
OUTPUT
SOLUTION
OUTPUT
SOLUTION
OUTPUT
SOLUTION
OUTPUT
SOLUTION
OUTPUT
SOLUTION
OUTPUT
SOLUTION
OUTPUT
SOLUTION
OUTPUT
SOLUTION
OUTPUT
LIFO, last in first out.
append().
pop().
With stack[-1].
|
A stack is a data structure with LIFO order: the last item pushed is the first popped. A Python list is used as a stack: append() pushes and pop() removes the top; stack[-1] peeks; the stack is empty when its length is zero. Stacks power undo, reversing and function calls. |
| Open the Virtual Lab |
These free Class 12 Computer Science notes explain the stack data structure, LIFO, push and pop using a Python list, peek and isEmpty, and where stacks are used with worked Python programs and practice, for CBSE students across India and the Gulf including the UAE, Saudi Arabia, Qatar, Oman, Kuwait and Bahrain.
© 2026 School Revise. All rights reserved. Original content aligned to the CBSE and NCERT Class 12 Computer Science syllabus. Unauthorised copying is not permitted.