Skip to main content
Unlisted page
This page is unlisted. Search engines will not index it, and only users having a direct link can access it.

Chapter 3: Stacks in Python 🚀

Mentor's Note: A Stack is one of the most fundamental structures in computer science. Every time you press "Undo" (Ctrl+Z) or hit the "Back" button in your browser, you are using a Stack! 💡


🌟 The Scenario: The Pizza Box Stack 🍕

Imagine you are at a pizza delivery hub in Surat.

  • The Logic: As new pizzas come out of the oven, they are stacked one on top of the other. 📦
  • The Rule: You can only take the Top box. To get to the bottom box, you must first remove all the ones above it. 📦
  • The Result: This is LIFO (Last In, First Out). The last pizza placed on the stack is the first one to be delivered. ✅

📖 Concept Explanation

1. What is a Stack?

A stack is a linear data structure that follows the principle of Last In, First Out (LIFO).

2. Core Operations 🛠️

OperationLogicPython Method
PUSHAdd an item to the top..append()
POPRemove the top item..pop()
PEEKLook at the top item without removing it.stack[-1]
isEmptyCheck if the stack is empty.len(stack) == 0

🎨 Visual Logic: LIFO in Action


💻 Implementation: The Stack Lab

# 🛒 Scenario: Managing a stack of boxes
# 🚀 Action: Implementing PUSH and POP

def push(stack, item):
stack.append(item)
print(f"Pushed: {item} 📥")

def pop(stack):
if len(stack) == 0:
print("Underflow! Stack is empty. ❌")
return None
return stack.pop()

# 🏗️ Testing the stack
my_stack = []
push(my_stack, "Box 1")
push(my_stack, "Box 2")

removed = pop(my_stack)
print(f"Removed: {removed} 📤")

# 🛍️ Outcome: Prints "Removed: Box 2" (LIFO)

📊 Sample Dry Run

StepOperationStack StateResult
1push(10)[10]10 is at Top
2push(20)[10, 20]20 is at Top
3pop()[10]20 is removed! ✅

📈 Technical Analysis

  • Underflow: Trying to pop from an empty stack. ⚠️
  • Overflow: Trying to push into a stack that has no more memory (rare in Python).

🎯 Practice Lab 🧪

Task: The String Reverser

Task: Use a stack to reverse a string (e.g., "HELLO" -> "OLLEH"). Hint: Push each letter into a stack, then pop them all out! 💡


💡 Board Exam Focus (CBSE Class 12) 👔

  • Important: You will likely have to write a complete Python script including push() and pop() functions for 3-5 marks. Practice the "Underflow" check!

💡 Pro Tip: "A stack is not just a data structure; it's a way of looking at the world. Last in, first out!" - Anonymous


📈 Learning Path

📍 Visit Us

🏫 VD Computer Tuition Surat

VD Computer Tuition
📍 Address
2/66 Faram Street, Rustompura
Surat395002, Gujarat, India
📞 Phone / WhatsApp
+91 84604 41384
🌐 Website

Computer Classes & Tuition — Areas We Serve in Surat

AdajanAlthanAmroliAthwaAthwalinesBhagalBhatarBhestanCanal RoadChowkCitylightDumasGaurav PathGhod Dod RoadHaziraJahangirpuraKamrejKapodraKatargamLimbayatMagdallaMajura GateMota VarachhaNanpuraNew CitylightOlpadPalPandesaraParle PointPiplodPunaRanderRing RoadRustampuraSachinSalabatpuraSarthanaSosyo CircleUdhnaVarachhaVed RoadVesuVIP Road
📞 Call Sir💬 WhatsApp Sir