Python Data Structures & Algorithms – Full Notes
+ Solved Questions
1. Lists
• Ordered, mutable collection
• Example:
lst = [1, 2, 3]
lst.append(4)
2. Tuples
• Ordered, immutable
t = (1, 2, 3)
3. Sets
• Unordered, unique elements
s = {1, 2, 3}
s.add(4)
4. Dictionaries
• Key-value pairs
d = {"a":1, "b":2}
5. Stacks
• LIFO (Last In First Out)
1
, stack = []
stack.append(1)
stack.pop()
6. Queues
• FIFO (First In First Out)
from collections import deque
q = deque()
q.append(1)
q.popleft()
7. Searching Algorithms
Linear Search
def linear_search(arr, x):
for i in range(len(arr)):
if arr[i] == x:
return i
return -1
Binary Search
def binary_search(arr, x):
low, high = 0, len(arr)-1
while low <= high:
mid = (low + high)//2
if arr[mid] == x:
return mid
elif arr[mid] < x:
low = mid + 1
else:
high = mid - 1
return -1
2
+ Solved Questions
1. Lists
• Ordered, mutable collection
• Example:
lst = [1, 2, 3]
lst.append(4)
2. Tuples
• Ordered, immutable
t = (1, 2, 3)
3. Sets
• Unordered, unique elements
s = {1, 2, 3}
s.add(4)
4. Dictionaries
• Key-value pairs
d = {"a":1, "b":2}
5. Stacks
• LIFO (Last In First Out)
1
, stack = []
stack.append(1)
stack.pop()
6. Queues
• FIFO (First In First Out)
from collections import deque
q = deque()
q.append(1)
q.popleft()
7. Searching Algorithms
Linear Search
def linear_search(arr, x):
for i in range(len(arr)):
if arr[i] == x:
return i
return -1
Binary Search
def binary_search(arr, x):
low, high = 0, len(arr)-1
while low <= high:
mid = (low + high)//2
if arr[mid] == x:
return mid
elif arr[mid] < x:
low = mid + 1
else:
high = mid - 1
return -1
2