-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
Introduction to Python
Python Basics
Python Syntax
Python Comments
Python Variables
Python Data Types
Python Casting
Python I/O
Python Operators
Cotrol Structures
Data Structures
Python Strings
Python Lists
Python Tuples
Python Dictionaries
Python Sets
Python Arrays
Python Bytes and Bytearray
Date and Time
Functions and Module
File Handling
Python OOP
Advanced Concepts
Python Scope
Python Modules
Python JSON
Python RegEx
Python PIP
Python Try...Except
Python String Formatting
Python User Input
Python VirtualEnv
Python Math
Python DSA
Python DSA
Lists and Arrays
Python Stacks
Python Queues
Linked Lists
Python Hash Tables
Python Trees
Python Binary Trees
Binary Search Trees
Python AVL Trees
Python Graphs
Searching Algorithms
Sorting Algorithms
A stack is a linear data structure that follows the LIFO (Last In, First Out) principle — the last element added is the first one to be removed.
Python doesn’t have a built-in stack data type, but stacks can be easily implemented using lists or the collections.deque
module.
stack = []
# Push elements
stack.append(10)
stack.append(20)
stack.append(30)
print(stack) # [10, 20, 30]
# Pop element
stack.pop() # Removes 30
print(stack) # [10, 20]
Method | Description |
---|---|
append(x) |
Push item onto stack |
pop() |
Pop top item from stack |
[-1] |
Peek top item (no remove) |
len() |
Stack size |
collections.deque
(Recommended)from collections import deque
stack = deque()
stack.append("a")
stack.append("b")
stack.append("c")
print(stack.pop()) # Output: c
✅ deque
is faster and more memory-efficient for stack operations than lists.
To check the top without removing it:
top = stack[-1]
print("Top element:", top)
if not stack:
print("Stack is empty")
else:
print("Stack is not empty")
Undo operations in editors
Backtracking in games
Expression evaluation
Function call stack
Q1. Write a Python program to create an empty stack and add three items to it.
Q2. Write a Python program to remove the last added item using pop()
.
Q3. Write a Python program to check the top item in the stack without removing it.
Q4. Write a Python program to print all items in a stack using a loop.
Q5. Write a Python program to use collections.deque
to push and pop names.
Q6. Write a Python program to count the number of items in a stack using len()
.
Q7. Write a Python program to create a stack and remove all items one by one using a loop.
Q8. Write a Python program to use a stack to reverse a string.
Q9. Write a Python program to check if the stack is empty and print a message accordingly.
Q10. Write a Python program to push and pop elements in numeric order and display the stack content at each step.
Introduction to Python
Python Basics
Python Syntax
Python Comments
Python Variables
Python Data Types
Python Casting
Python I/O
Python Operators
Cotrol Structures
Data Structures
Python Strings
Python Lists
Python Tuples
Python Dictionaries
Python Sets
Python Arrays
Python Bytes and Bytearray
Date and Time
Functions and Module
File Handling
Python OOP
Advanced Concepts
Python Scope
Python Modules
Python JSON
Python RegEx
Python PIP
Python Try...Except
Python String Formatting
Python User Input
Python VirtualEnv
Python Math
Python DSA
Python DSA
Lists and Arrays
Python Stacks
Python Queues
Linked Lists
Python Hash Tables
Python Trees
Python Binary Trees
Binary Search Trees
Python AVL Trees
Python Graphs
Searching Algorithms
Sorting Algorithms