Chapter : Stack - Data Structure in Python
Why Are Data Structures Important?
Data structures help us decide how data should be arranged in memory so that a program
can work with it efficiently.
Here’s what you need to know:
Data structures are the foundation of computer science. They help you write programs
that are fast, efficient, and organized—no matter which programming language you're
using.
A data structure is a way to group and organize data (sometimes of different types) so
that it can be handled as one single unit.
Every data structure has a set of well-defined operations (like insert, delete, search), a
particular behavior, and some unique properties.
Data Type Vs Data Structure
What is a Data Type?
A data type defines what kind of data a variable can hold.
It tells the computer what type of value is stored and how much memory is needed.
Examples:
int – stores whole numbers like 10, -5
float – stores decimal numbers like 3.14
str – stores text like "Hello"
bool – stores either True or False
Think of data types as basic building blocks.
They help define the type and nature of a single piece of data.
What is a Data Structure?
A data structure is a way of organizing and storing a group of data so it can be used
efficiently.
It’s like a container that holds multiple values, often of the same or different data types.
Examples:
List – stores a collection of values like [1, 2, 3]
Tuple – stores fixed values like (10, 20)
Dictionary – stores key-value pairs like {"name": "John"}
Stack, Queue, Tree – special ways to organize data for faster operations
Think of data structures as containers or folders that store multiple values together in a
specific format.
What is a Stack?
The logical or mathematical model of a particular organization of data is called data
structure. It is a way of storing, accessing
A stack is a data structure that follows the LIFO (Last In First Out) order.
Think of a scenario where at a dinner party where there is a stack of
plates, plates are always added or removed from the top of the pile
LIFO: The last element added is the first one to be removed.
Manipulating data. List: An array or list is the collection of elements
in ordered way
Implementation of stack using list
• The implementation of stack using list in Python is the easiest of all programming language.
• Basic operations performed on stack are:
1. Creating a stack
2. Push/Adding elements to the stack
3. Checking for empty stack
4. Pop/Deleting elements from a stack
5. Traversal/Displaying a stack
List methods used and Important things to remember
1) [Link](element) – It is used to implement push operations(used to append or
add elements at the end of the list)
2) [Link]() –It is used to implement pop operations(removing elements at the end)
3) list[::-1]-List slicing is used to print the elements in the reverse order from top
position to list[0]
4) top=len(list)-1 (length of list -1)
5) stack - LIFO(Last In First Out)
6) Push and Pop through one end(Top)
Real-life Analogy:
Plates in a rack
Undo feature in MS Word
Browser back button
Applications of Stack:
Undo/Redo operations
Backtracking (puzzles, mazes)
Function call management (recursion)
Expression evaluation (Postfix, Prefix)
[Link] Initialization
Example:
[Link]() – Add an element to the stack
Use append() function to add an item on top.
Example:
[Link]() – Remove the top element from the stack
Removes and returns the last element.
4. peek() – View the top element without removing it
Use negative indexing.
5. isEmpty() – Check if the stack is empty
6. isFull() – (Only for fixed size stacks)
Python Code Description
Operation
push [Link](item) Adds item to the top
pop [Link]() Removes and returns top item
peek/top stack[-1] Returns top item without removing
isEmpty len(stack)==0 or not stack Checks if stack is empty
isFull len(stack)==MAX_SIZE Checks if stack is full (if bounded)
Example Code:
Stack Overflow:
Meaning: Trying to add (push) an element to a stack that is already full.
Happens when: Stack has a fixed size and no more space left.
Result: Error – “Stack Overflow”
Stack Underflow:
Meaning: Trying to remove (pop) an element from a stack that is empty.
Happens when: There is nothing to remove.
Result: Error – “Stack Underflow”