0% found this document useful (0 votes)
2 views

Data Structure in Python

The document provides an overview of data structures in Python, categorizing them into linear and non-linear structures, and introduces Python-specific data structures like lists, tuples, and dictionaries. It details the implementation of stacks and queues in Python, including their operations and associated functions. The document also includes code snippets for stack and queue operations, demonstrating how to manage these data structures effectively in Python.

Uploaded by

itclubcvp2223
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Data Structure in Python

The document provides an overview of data structures in Python, categorizing them into linear and non-linear structures, and introduces Python-specific data structures like lists, tuples, and dictionaries. It details the implementation of stacks and queues in Python, including their operations and associated functions. The document also includes code snippets for stack and queue operations, demonstrating how to manage these data structures effectively in Python.

Uploaded by

itclubcvp2223
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Data Structure in Python

By:-
Amit Yerpude
PGT(Computer Science)
Kendriya Vidyalaya, Khagaria
Presented By: - Amit Yerpude, 6/25/2020
PGT(CS), KV Khagaria

Data Structure Overview

• Data structures are fundamental concepts of computer science


which helps is writing efficient programs in any language.

• Python is a high-level, interpreted, interactive and object-oriented


scripting language using which we can study the fundamentals of
data structure in a simpler way as compared to other
programming languages.
Presented By: - Amit Yerpude, 6/25/2020
PGT(CS), KV Khagaria

• The various data structures in computer science are divided broadly into two
categories shown below.
• Liner Data Structures
 These are the data structures which store the data elements in a sequential manner.
 Array: It is a sequential arrangement of data elements paired with the index of the
data element.
 Linked List: Each data element contains a link to another element along with the data
present in it.
 Stack: It is a data structure which follows only to specific order of operation. LIFO(last
in First Out) or FILO(First in Last Out).
 Queue: It is similar to Stack but the order of operation is only FIFO(First In First Out).
 Matrix: It is two dimensional data structure in which the data element is referred by a
pair of indices.
Presented By: - Amit Yerpude, 6/25/2020
PGT(CS), KV Khagaria

• Non-Liner Data Structures


 These are the data structures in which there is no sequential linking of data
elements. Any pair or group of data elements can be linked to each other and
can be accessed without a strict sequence.
 Binary Tree: It is a data structure where each data element can be connected
to maximum two other data elements and it starts with a root node.
 Heap: It is a special case of Tree data structure where the data in the parent
node is either strictly greater than/ equal to the child nodes or strictly less than
it’s child nodes.
 Hash Table: It is a data structure which is made of arrays associated with each
other using a hash function. It retrieves values using keys rather than index
from a data element.
 Graph: .It is an arrangement of vertices and nodes where some of the nodes
are connected to each other through links.
Presented By: - Amit Yerpude, 6/25/2020
PGT(CS), KV Khagaria

Python Specific Data Structures


• These data structures are specific to python language and they
give greater flexibility in storing different types of data and faster
processing in python environment.
 List: It is similar to array with the exception that the data elements can
be of different data types. You can have both numeric and string data in a
python list.
 Tuple: Tuples are similar to lists but they are immutable which means
the values in a tuple cannot be modified they can only be read.
 Dictionary: The dictionary contains Key-value pairs as its data
elements.
Presented By: - Amit Yerpude, 6/25/2020
PGT(CS), KV Khagaria

Stack in Python
• A stack is a linear data structure that stores items in a Last-
In/First-Out (LIFO) or First-In/Last-Out (FILO) manner.
• In stack, a new element is added at one end and an element is
removed from that end only.
• The insert and delete operations are often called push and pop.
Presented By: - Amit Yerpude, 6/25/2020
PGT(CS), KV Khagaria

The functions associated with stack are:

• empty() – Returns whether the stack is empty – Time


Complexity : O(1)
• size() – Returns the size of the stack – Time Complexity : O(1)
• top() – Returns a reference to the top most element of the stack –
Time Complexity : O(1)
• push(g) – Adds the element ‘g’ at the top of the stack – Time
Complexity : O(1)
• pop() – Deletes the top most element of the stack – Time
Complexity : O(1)
Presented By: - Amit Yerpude, 6/25/2020
PGT(CS), KV Khagaria

stack implementation in Python


def isempty(stk): def pop(stk):
def push(stk, data): if isempty(stk):
if stk==[]: stk.append(data) return "underflow"
return True top=len(stk)-1 else:
else: data=stk.pop()
return False if len(stk)==0:
top=None
else:
top=len(stk)-1
return data
Presented By: - Amit Yerpude, 6/25/2020
PGT(CS), KV Khagaria

def peek(stk): def display(stk):


if isempty(stk): if isempty(stk):
return "underflow" return "underflow"
else: else:
top=len(stk)-1 top=len(stk)-1
return stk[top] for a in range(top, -1,-1):
print(stk[a],end="")
# __main__
stk=[]
Presented By: - Amit Yerpude, 6/25/2020
top=None PGT(CS), KV Khagaria

while True:
print("STACK OPERATION MENU")
print("1. PUSH IN STACK")
print("2. POP FROM STACK")
print("3. PEEK TOPMOST ELEMENT")
print("4. DISPLAY STACK ELEMENT")
print("5. EXIT PROGRAM")
choice=int(input("Enter Your Choice(1-5):- "))
if choice ==1:
data=int(input("Enter data to push in stack:- "))
push(stk,data)
elif choice==2:
data=pop(stk)
print("Popped element from top of stack = ",data)
elif choice==3:
data=peek(stk)
print("Top most element in stack is ",data)
elif choice==4:
display(stk)
elif choice == 5:
break
else:
print("Invalid Choice")
Presented By: - Amit Yerpude, 6/25/2020
PGT(CS), KV Khagaria

implement a queue in Python

• A queue is a first-in-first
out(FIFO) data structure. It
has three primitive operations:
 enqueue: Add an element to
the queue
 dequeue: Remove an element
from the queue
 Peek: Get the top most element
of the queue.i.e, the element at
the front position.
Presented By: - Amit Yerpude, 6/25/2020
PGT(CS), KV Khagaria

• In Python, a queue is implemented using a list object.


▫ To push an item in the queue, use the list function append
list.append(item)
▫ To pop an item from the queue, use the list function pop list.pop(0)
▫ To get the top most item in the queue, get the last index of the list
using: list[-1]
Presented By: - Amit Yerpude, 6/25/2020
PGT(CS), KV Khagaria

implementation of Queue
def isempty(Qu): def Enqueue(Qu, data): def Dequeue(Qu):
if Qu==[]: Qu.append(data) if isempty(Qu):
return True if len(Qu)==1: print("Underflow or
else: front=rear=0 Queue is Empty")
else: else:
return False
rear=len(Qu)-1
data=Qu.pop(0)
if len(Qu)==0:
front=rear=None
return data
Presented By: - Amit Yerpude, 6/25/2020
PGT(CS), KV Khagaria

def Peek(Qu): def Display(Qu):


if isempty(Qu): if isempty(Qu):
print("Queue is Empty") print("Queue Underflow")
else: else:
return Qu[0] for a in range(len(Qu)):
print(Qu[a],end=" ")
#__main__
myque=[10,20,30]
front=None Presented By: - Amit Yerpude,
PGT(CS), KV Khagaria
6/25/2020

rear=None
while True:
print("QUEUE OPERATION MENU")
print("1. ENQUE")
print("2. DEQUE")
print("3. PEEK")
print("4. DISPLAY")
print("5. EXIT")
choice=int(input("Enter Your Choice(1-5):- "))
if choice ==1:
data=int(input("Enter data to insert in queue- "))
Enqueue(myque,data)
elif choice==2:
data=Dequeue(myque)
print("Dequeued element from front of queue = ",data)
elif choice==3:
data=Peek(myque)
print("Element at front of queue is ",data)
elif choice==4:
Display(myque)
elif choice == 5:
break
else:
print("Invalid Choice")
Presented By: - Amit Yerpude, 6/25/2020
PGT(CS), KV Khagaria
Presented By: - Amit Yerpude, 6/25/2020
PGT(CS), KV Khagaria

Thank you!!!

You might also like