Python 101
Basics:
Type () 🡪 a function that returns data type
In python input function always returns string value so if it's a number then you are supposed
to change it into integer or float.
Loop will run until the condition remains True
and: Both the condition should be true
or: any one condition should be true
/ is used for normal div
// is used for Div (int)
% is used for Mod
Length = len()
Concept Of Slicing 🡪 Separate "Papers" ; name = Papersdock ; name[0:6] ; Note Space is also
considered; separate “docks” ; name[6:10]
Arrays:
EmptyArray = [""] * 500 🡪 This creates a 1D array called EmptyArray with 500 elements, where
each element is an empty string.
[Link](" ") 🡪 adds value in an array
2d arrays
Empty2d = [[""] * 40 for i in range(500)] 🡪 This line of code creates a 2D array with 500 rows
and 40 columns with the name Empty2d containing Empty Strings
We use the concepts of Nested Loops to access the individual elements; Outer loop will be for
Rows and Inner Loop will be for Columns 🡪 asseccing
Random:
Import random
[Link]
Files:
File = open(“filename”,”r”) 🡪 read mode
text = [Link]()
[Link]() 🡪 reads file line by line
.strip() 🡪 used to remove extra white space character
file = open( "[Link]" , "w" ) 🡪 write mode
[Link]( name + “\n")
file = open( "[Link]" , "a" ) 🡪 append mode
[Link]("Taha\n") 🡪 same for write and append
[Link]()🡪 close file
file = open ("[Link]" , "r")
for line in file:
print([Link]()) ; The data of line will be automatically stored in line variable 🡪 don’t know the
number of lines in file
Exceptional Handling:
try :
code which might contain exception
except:
print (“error”)
there are ValueError , ZeroDivisionError , IOError
example; except IOError:
bubble sort:
Insertion sort:
Notes:
Start from 2nd element
Store value
Compare with left side
If the previous element is bigger, move it one position to the right.
Keep comparing and shifting left-side elements until:
● You find a smaller value, OR
● You reach the start of the array.
Insert valueToInsert into the correct position (the “hole”).
Linear searching:
Binary searching:
Stacks (LIFO):
Queue (FIFO):
Linear queue:
Notes:
ENQUEUE (Add)
1. Check if queue is full
2. If full → print message
3. Else:
o Add value at tailpointer
o Increase tailpointer
o If first element → set headpointer = 0
DEQUEUE (Remove)
1. Check if queue is empty
2. If empty → print message
3. Else:
o Output value at headpointer
o Clear value
o Move headpointer forward
4. If queue becomes empty:
o Reset both pointers
Circular queue:
Notes:
ENQUEUE
1. Check if queue is full (numberOfItems == max)
2. If full → return False
3. Insert value at tailpointer
4. Move tail forward
5. If end reached → go back to 0 (circular)
6. Increase numberOfItems
DEQUEUE
1. Check if queue is empty (numberOfItems == 0)
2. If empty → return False
3. Take value from headpointer
4. Clear that space
5. Move head forward
6. If end reached → go back to 0
7. Decrease numberOfItems
Linked list:
ADD NODE (unordered):
✅ 1. Check if list is full
● If emptyList == -1
→ ❌ No space → return False
✅ 2. Take a free node
● Set freelist = emptyList
● Move emptyList to next free node
✅ 3. Store data
● Create new node with:
o data = input value
o nextNode = -1
● Put it into linkedList[freelist]
✅ 4. Traverse to end of list
● Move through nodes until:
o currentpointer == -1
● Keep track of previous node
✅ 5. Link new node
● Set:
linkedList[previouspointer].nextNode = freelist
✅ 6. Finish
● Return True
Delete node:
✅ 1. Start at beginning
● currentpointer = startPointer
● Set previouspointer = -1 or 0
✅ 2. Search for value
● Move through list until:
o value found OR
o reach end (-1)
✅ 3. Check if found
● currentpointer == -1
● If not found → return False
✅ 4. Delete node
👉 If first node:
● Move startPointer to next node
👉 Else:
● Link previous node to next node
linkedList[previouspointer].nextNode = linkedList[currentpointer].nextNode
✅ 5. Add deleted node to free list
● Set data = 0
● Set nextNode = emptyList
● Update emptyList
● Use currentpointer
✅ 6. Return True
Ordered linked list insertion:
ORDERED INSERT:
✅ 1. Check if full
● If emptyList == -1 → return False
✅ 2. Take free node
● freelist = emptyList
● Move emptyList forward
✅ 3. Add data
● Create node (data, -1)
● Store in linkedList[freelist]
✅ 4. Find correct position
● Traverse list while:
o not end AND
o current data < new data
✅ 5. Insert node
👉 If at beginning:
● New node → points to start
● Update startPointer by pointing it to freelist index position
👉 Else:
● New node → points to current
● Previous → points to new node
✅ 6. Return True
Binary tree:
Add Node:
✅ 1. Check if tree is full
● If FreeNode < max index
✅ 2. Create new node
At FreeNode:
● Left pointer = -1
● Data = value to insert
● Right pointer = -1
✅ 3. Check if tree is empty
● If RootPointer == -1
→ Set RootPointer = FreeNode
✅ 4. Otherwise, find position
● Start at CurrentPointer = RootPointer
● Repeat until placed: (placed = false)
✅ 5. Compare values
👉 If data < current node:
● Move left
👉 If data >= current node: (else)
● Move right
✅ 6. Check if position is empty
👉 If pointer is -1:
● Insert node there
● Set pointer = FreeNode
● Stop
👉 Else:
● Increment currentPointer
● Repeat
✅ 7. Update FreeNode
● FreeNode = FreeNode + 1
Search Node:
✅ 1. Start at root
● CurrentPointer = RootPointer
✅ 2. Repeat until:
● Node is found OR
● CurrentPointer == -1 (end reached)
✅ 3. Compare values
👉 If data == current node
● ✅ Return position (found)
👉 If data < current node
● Move left
👉 If data > current node
● Move right
✅ 4. If not found
● Return -1
Recursion:
Inheritance:
✅ 1. Create parent class
● Add common attributes
● Add common methods
✅ 2. Create child class
class Child(Parent):
constructor : def __init__(self, parameters of parent, parameters of child):
✅ 3. Use super()
● Call parent constructor
● Super().__init__(parameters of parent class)
✅ 4. Add extra attributes/methods
● Specific to child class
When to Use super()
✅ Use super() when:
👉 You want to ADD to parent behaviour
Example keyword:
👉 “Additionally” / “also” / “extend”
❌ Do NOT use super() when:
👉 You want to completely replace parent method
Other OOP notes from PD