0% found this document useful (0 votes)
4 views7 pages

Python Data Structures Practical Guide

The document outlines practical exercises for basic data structures in Python, including list operations, tuple methods, dictionary manipulations, stack and queue implementations, and search algorithms. Each practical includes code snippets and expected outputs for operations such as indexing, slicing, appending, sorting, and searching. The exercises aim to provide hands-on experience with fundamental data structures and their functionalities.

Uploaded by

ayankhanbaloch01
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

Python Data Structures Practical Guide

The document outlines practical exercises for basic data structures in Python, including list operations, tuple methods, dictionary manipulations, stack and queue implementations, and search algorithms. Each practical includes code snippets and expected outputs for operations such as indexing, slicing, appending, sorting, and searching. The exercises aim to provide hands-on experience with fundamental data structures and their functionalities.

Uploaded by

ayankhanbaloch01
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Unit-II Basic Data Structures in Python

Practical list
✅ Practical 1: List Operations (Indexing, Slicing, Traversing)
Program
lst = [10, 20, 30, 40, 50]

print("Original List:", lst)


print("First element:", lst[0])
print("Last element:", lst[-1])
print("Slicing (2:4):", lst[2:4])

# Traversing
for i in lst:
print(i, end=" ")
●​ Output
Original List: [10, 20, 30, 40, 50]
First element: 10
Last element: 50
Slicing (2:4): [30, 40]
10 20 30 40 50
✅ Practical 2: List Methods (append, extend, insert, remove, pop, sort)
Program
lst = [5, 2, 9]
[Link](10)
[Link]([7, 8])
[Link](1, 15)
[Link](2)
popped = [Link]()
[Link]()
print("Updated List:", lst)
print("Popped Element:", popped)
●​ Output
Updated List: [5, 7, 8, 9, 10, 15]
Popped Element: 8
✅ Practical 3: Nested List Traversal
Program
matrix = [[1,2,3],[4,5,6],[7,8,9]]
for row in matrix:
for elem in row:
print(elem, end=" ")
print()
●​ Output
123
456
789
✅ Practical 4: Tuple Operations and Methods
Program
t = (1, 2, 3, 2, 4)
print("Tuple:", t)
print("Count of 2:", [Link](2))
print("Index of 3:", [Link](3))
print("Sorted Tuple:", sorted(t))
print("Min:", min(t), "Max:", max(t))
●​ Output
Tuple: (1, 2, 3, 2, 4)
Count of 2: 2
Index of 3: 2
Sorted Tuple: [1, 2, 2, 3, 4]
Min: 1 Max: 4

✅ Practical 5: Dictionary Operations


Program
student = {"name":"Amit", "age":21, "course":"BSc CS"}
student["age"] = 22
student["city"] = "Mumbai"
print("Dictionary:", student)
print("Keys:", [Link]())
print("Values:", [Link]())
print("Items:", [Link]())
[Link]("city")
print("After pop:", student)
●​ Output
Dictionary: {'name': 'Amit', 'age': 22, 'course': 'BSc CS', 'city': 'Mumbai'}
Keys: dict_keys(['name', 'age', 'course', 'city'])
Values: dict_values(['Amit', 22, 'BSc CS', 'Mumbai'])
Items: dict_items([('name', 'Amit'), ('age', 22), ('course', 'BSc CS'), ('city',
'Mumbai')])
After pop: {'name': 'Amit', 'age': 22, 'course': 'BSc CS'}

✅ Practical 6: Stack Implementation Using List


Program
stack = []
[Link](10) # push
[Link](20)
[Link](30)
print("Stack:", stack)
[Link]() # pop
print("Stack after pop:", stack)
●​ Output
Stack: [10, 20, 30]
Stack after pop: [10, 20]
✅ Practical 7: Queue Implementation Using List
Program
queue = []
[Link](10) # enqueue
[Link](20)
[Link](30)
print("Queue:", queue)
[Link](0) # dequeue
print("Queue after dequeue:", queue)
●​ Output
Queue: [10, 20, 30]
Queue after dequeue: [20, 30]

✅ Practical 8: Bubble Sort


Program
arr = [64, 34, 25, 12, 22, 11, 90]
n = len(arr)

for i in range(n):
for j in range(0, n-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]

print("Sorted Array:", arr)


●​ Output
Sorted Array: [11, 12, 22, 25, 34, 64, 90]

✅ Practical 9: Linear Search


Program
arr = [5, 3, 8, 6, 7]
x=6
found = False
for i in range(len(arr)):
if arr[i] == x:
found = True
print("Element found at index", i)
break
if not found:
print("Element not found")
●​ Output
Element found at index 3

✅ Practical 10: Binary Search (Sorted List)


Program
arr = [2, 4, 6, 8, 10, 12]
x = 10
low = 0
high = len(arr)-1
found = False
while low <= high:
mid = (low+high)//2
if arr[mid] == x:
found = True
print("Element found at index", mid)
break
elif arr[mid] < x:
low = mid + 1
else:
high = mid - 1

if not found:
print("Element not found")
●​ Output
Element found at index 4

You might also like