Python Program to Implement a Stack

A stack is a Last-In-First-Out (LIFO) data structure where elements are added and removed from the same end (the top). In Python, we can implement a stack using a class with methods for push, pop, and checking if the stack is empty.

Stack Implementation

Here's a complete stack implementation with push, pop, and empty check operations ?

class Stack:
    def __init__(self):
        self.items = []
    
    def is_empty(self):
        return len(self.items) == 0
    
    def push(self, data):
        self.items.append(data)
        print(f"Pushed {data} to stack")
    
    def pop(self):
        if self.is_empty():
            return "Stack is empty"
        return self.items.pop()
    
    def peek(self):
        if self.is_empty():
            return "Stack is empty"
        return self.items[-1]
    
    def display(self):
        if self.is_empty():
            return "Stack is empty"
        return self.items

# Create a stack instance
stack = Stack()

# Demonstrate stack operations
stack.push(10)
stack.push(20)
stack.push(30)

print("Current stack:", stack.display())
print("Top element:", stack.peek())

print("Popped element:", stack.pop())
print("Popped element:", stack.pop())

print("Current stack:", stack.display())
print("Is stack empty?", stack.is_empty())

print("Popped element:", stack.pop())
print("Is stack empty?", stack.is_empty())
Pushed 10 to stack
Pushed 20 to stack
Pushed 30 to stack
Current stack: [10, 20, 30]
Top element: 30
Popped element: 30
Popped element: 20
Current stack: [10]
Is stack empty? False
Popped element: 10
Is stack empty? True

Key Stack Operations

  • Push: Adds an element to the top of the stack using append()

  • Pop: Removes and returns the top element using pop()

  • Peek/Top: Returns the top element without removing it

  • is_empty: Checks if the stack has no elements

  • Display: Shows all elements in the stack

Stack with Size Limit

Here's an enhanced stack implementation with a maximum size limit ?

class LimitedStack:
    def __init__(self, max_size):
        self.items = []
        self.max_size = max_size
    
    def is_empty(self):
        return len(self.items) == 0
    
    def is_full(self):
        return len(self.items) == self.max_size
    
    def push(self, data):
        if self.is_full():
            return "Stack overflow! Cannot push more elements"
        self.items.append(data)
        return f"Pushed {data} to stack"
    
    def pop(self):
        if self.is_empty():
            return "Stack underflow! No elements to pop"
        return self.items.pop()
    
    def size(self):
        return len(self.items)

# Create a stack with maximum size of 3
limited_stack = LimitedStack(3)

print(limited_stack.push(1))
print(limited_stack.push(2))
print(limited_stack.push(3))
print(limited_stack.push(4))  # Should show overflow

print("Stack size:", limited_stack.size())
print("Popped:", limited_stack.pop())
print("Stack size:", limited_stack.size())
Pushed 1 to stack
Pushed 2 to stack
Pushed 3 to stack
Stack overflow! Cannot push more elements
Stack size: 3
Popped: 3
Stack size: 2

Common Use Cases

  • Function Call Management: Programming languages use stacks to manage function calls

  • Expression Evaluation: Converting infix to postfix notation and evaluating expressions

  • Undo Operations: Text editors and applications use stacks for undo functionality

  • Browser History: Web browsers use stacks to track page history

Conclusion

A stack is a fundamental data structure that follows LIFO principle. Python lists provide built-in append() and pop() methods that make stack implementation straightforward and efficient.

---
Updated on: 2026-03-25T18:59:04+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements