On this page
The stack is the one data structure in the CBSE Class 12 computer science syllabus, and it reliably turns up as a three-mark question. The code is short, but marks go on small things: popping from an empty stack, pushing the wrong items, or printing in the wrong order. This post explains what a stack is, builds one in Python, solves a board-style question and shows three places stacks are used.
What does the syllabus ask about stacks?
The CBSE Computer Science curriculum for Class XII (2026-27) lists "Data Structure: Stack, operations on stack (push & pop), implementation of stack using list" in Unit 1, which carries 40 of the 70 theory marks. Its suggested practical list includes "Write a Python program to implement a stack using list." The NCERT chapter on stacks, 2026-27 reprint, goes further, into arithmetic expressions in infix and postfix notation, so read it for understanding even where the syllabus line is short.
What is a stack?
A stack is a list where you add and remove items at one end only, called the top. The last item added is the first one out, which is called LIFO (last in, first out). NCERT's picture is a pile of glasses: you put a new glass on top and take the top one off first.
Two operations define it:
Push adds an item to the top.
Pop removes the item from the top and returns it.
Trying to pop from an empty stack is called underflow. NCERT notes that a Python list has no fixed size, so a list-based stack will not overflow unless memory runs out.
The Python tutorial says the list methods "make it very easy to use a list as a stack": use append() to add an item to the top and pop() without an index to take it off.
How do you write a stack in Python?
def is_empty(stk):
return len(stk) == 0
def push(stk, item):
stk.append(item)
def pop(stk):
if is_empty(stk):
return "Underflow"
return stk.pop()
def peek(stk):
if is_empty(stk):
return None
return stk[-1]The end of the list is the top. peek() reads the top item without removing it. Always check for an empty stack before pop() or peek(), because calling pop() on an empty list raises an IndexError.
How do you answer a board-style stack question?
The CBSE sample question paper for 2026-27 has a typical three-mark version: given a list of records, write one function that pushes only the records meeting a condition, and another that pops and displays them all, then prints "Stack Empty". Here is the same pattern with different data.
Question. A list holds students' names and marks. Push the students with 75 or more marks onto a stack called Toppers. Then pop and display them, and print "Stack Empty" at the end.
results = [("Asha", 82), ("Ravi", 64), ("Meera", 91), ("Kabir", 75)]
Toppers = []
def push_toppers():
for name, marks in results:
if marks >= 75:
push(Toppers, (name, marks))
def pop_toppers():
while not is_empty(Toppers):
print(pop(Toppers))
print("Stack Empty")
push_toppers()
pop_toppers()Output:
('Kabir', 75)
('Meera', 91)
('Asha', 82)
Stack Empty
Ravi is never pushed. The order is reversed compared with the list, because Kabir went on last and comes off first. Examiners check exactly this.
Where are stacks used?
Reversing a string. Push every character, then pop them all.
def reverse_string(text):
stk = []
for ch in text:
stk.append(ch)
out = ""
while stk:
out += stk.pop()
return out
print(reverse_string("STACK")) # KCATSChecking brackets. NCERT gives matching parentheses as a use of stacks, in the way a compiler checks code. Push each opening bracket; at each closing bracket, pop and check that it matches. The expression is balanced only if every pop matches and the stack is empty at the end. So "(a+b)*[c-d]" is balanced, while "(a+b]" and "((a)" are not.
Evaluating postfix expressions. In postfix notation the operator comes after its operands, so "6 2 3 * +" means 6 + (2 × 3). NCERT's method: read left to right; push numbers; at an operator, pop two numbers, apply it and push the result. For "6 2 3 * +": push 6, 2, 3; at *, pop 3 and 2 and push 6; at +, pop 6 and 6 and push 12. The answer is 12. For "8 2 − 3 *", the steps give 8 − 2 = 6, then 6 × 3 = 18. With subtraction and division, the second number popped goes first.
What mistakes cost marks?
No empty check before pop(), which raises an IndexError on an empty list.
Using pop(0), which removes from the front. That makes a queue, not a stack.
Pushing everything when the question asks only for items that meet a condition.
Missing the final message, such as "Stack Empty", when the question asks for it.
Wrong operand order in postfix subtraction or division.
A practice routine for stacks
Day 1: Write is_empty, push, pop and peek from memory, then test each one, including pop on an empty stack.
Day 2: Write three conditional push and pop pairs: numbers divisible by 3, words longer than five letters, and records with a price above a limit.
Day 3: Trace two postfix expressions by hand, then check them with code.
Weekly: One stack question from a sample paper in under 10 minutes.
Stacks also appear in the practical exam and its viva. Our notes on the board practical exam and viva cover how to prepare. For the rest of Unit 1, read the Python basics that are tested.
If the idea of LIFO or postfix notation is not clear yet, the Fermi Concept Library has every concept in computing with its own lesson, so you can understand the structure before you write the code.
Write the four stack functions from memory today, then push five items and pop them to check the order.



