Curriculum
Course: Grade XII Computer Science
Login
Text lesson

Ch-3 Stack

School Revise · Class 11 Computer Science · Chapter 3

Stack

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.

LIFO

last in, first out

list

used as a stack

What this chapter is about

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.

1. Push and pop with a list

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

2. Peek and isEmpty

Peek looks at the top item without removing it, using stack[-1]. The stack is empty when its length is zero.

3. Where stacks are used

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.

Practise with the interactive

Explore the idea by tapping. The interactive opens right here in the lesson.

Try it in the code lab

Write and run real Python right here in the lesson, then work through the practice problems with answers.

Starting Python...

Coding practice problems, with answers

Type each one into the code lab above, then open the card to see the worked solution and its output.

Problem 1. Push items onto a stack.

SOLUTION

stack = [] stack.append(10) stack.append(20) print(stack)

OUTPUT

[10, 20]
Problem 2. Pop the top item.

SOLUTION

stack = [10, 20, 30] print(stack.pop()) print(stack)

OUTPUT

30 [10, 20]
Problem 3. Peek at the top item.

SOLUTION

stack = [10, 20, 30] print(stack[-1])

OUTPUT

30
Problem 4. Check if a stack is empty.

SOLUTION

stack = [] print(len(stack) == 0)

OUTPUT

True
Problem 5. Push three items and print the stack.

SOLUTION

stack = [] for x in [1, 2, 3]: stack.append(x) print(stack)

OUTPUT

[1, 2, 3]
Problem 6. Pop all items to see the LIFO order.

SOLUTION

stack = [1, 2, 3] while stack: print(stack.pop())

OUTPUT

3 2 1
Problem 7. Find the size of a stack.

SOLUTION

stack = [5, 6, 7] print(len(stack))

OUTPUT

3
Problem 8. Reverse a string using a stack.

SOLUTION

s = “abc” stack = list(s) result = “” while stack: result = result + stack.pop() print(result)

OUTPUT

cba
Problem 9. Write a push function.

SOLUTION

def push(stack, item): stack.append(item) s = [] push(s, 5) print(s)

OUTPUT

[5]
Problem 10. Write a pop function with an empty check.

SOLUTION

def pop(stack): if len(stack) == 0: return “empty” return stack.pop() print(pop([1, 2])) print(pop([]))

OUTPUT

2 empty

Practice set A, multiple choice

1. A stack works on which principle?

LIFO, last in first out.

2. Which list method pushes an item onto the stack?

append().

3. Which list method removes the top item?

pop().

4. How do you peek at the top item?

With stack[-1].

Quick summary

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.

Layer 1
Login Categories