1️⃣ Basics & Syntax
📌 Dynamic Typing
n = 0
n = "abc" # valid
No type declaration
Type decided at runtime
📌 Multiple Assignment
a, b = 1 , "hello"
📌 Increment
n += 1
# ❌ n++ not allowed
📌 Null Value
x = None
📌 Conditionals
if x > 0 :
print ( "positive" )
elif x == 0 :
print ( "zero" )
else :
print ( "negative" )
No parentheses
Use indentation
Use : after condition
📌 Logical Operators
if a > 0 and b > 0 :
2️⃣ Loops
📌 While Loop
i = 0
while i < 5 :
print (i)
i += 1
📌 For Loop
for i in range ( 5 ): # 0 → 4
for i in range ( 2 , 6 ): # 2 → 5
for i in range ( 5 , 1 , - 1 ): # reverse 5 → 2
3️⃣ Numbers & Math
📌 Division
5 / 2 # 2.5
5 // 2 # 2 (floor)
⚠️ Negative case:
- 3 // 2 # -2 (floor)
# most language round down to -1
# workaround
int ( - 3 / 2 ) # -1 (floor)
📌 Modulo
10 % 3 # 1
- 10 % 3 # 2 (important!)
math.fmod( - 10 , 3 )
📌 Useful Functions
import math
math.floor(x)
math.ceil(x)
math.sqrt(x)
pow ( 2 , 3 )
float ( "inf" )
float ( "-inf" )
4️⃣ Lists (Arrays) - Stack
📌 Basics
nums = [ 1 , 2 , 3 ]
nums.append( 4 ) # O(1)
nums.pop() # O(1)
📌 Insert
nums.insert( 1 , 10 ) # O(n)
📌 Access
nums[ 0 ]
nums[ - 1 ] # last element
📌 Initialize
arr = [ 1 ] * 5
📌 Slicing
nums[ 1 : 3 ] # index 1 → 2
📌 unpacking
a,b,c = [ 1 , 2 , 3 ]
📌 Looping
for num in nums:
for i in range ( len (nums)):
for i, val in enumerate (nums):
📌 Reverse & Sort
nums.reverse()
nums.sort()
nums.sort( reverse = True )
📌 Custom Sort
strs.sort( key =lambda x: len (x)) # by len of the string
📌 List Comprehension
[expression for item in iterable if condition]
squares = [x * x for x in nums]
even_numbers = [x for x in range ( 10 ) if x % 2 == 0 ]
matrix = [[ 1 , 2 ], [ 3 , 4 ], [ 5 , 6 ]]
flat = [num for row in matrix for num in row] # [1, 2, 3, 4, 5, 6]
Pattern Meaning [x for x in arr]loop [x for x in arr if cond]filter [f(x) for x in arr]transform [f(x) for x in arr if cond]filter + transform [x for a in A for x in a]nested loop [x if cond else y for x in arr]if-else [num for row in matrix for num in row]flat matrix
⚠️ 2D Array Pitfall
grid = [[ 0 ] * 4 for _ in range ( 4 )] # ✅ correct
grid = [[ 0 ] * 4 ] * 4 # ❌ same row reference
grid[ 0 ][ 0 ] = 1 # [[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]]
Case Use 1D list [0]*n ✅2D list [[0]*n for i in range(m)] ✅
5️⃣ Strings
📌 Basics
s = "hello"
📌 Operations
s + "world"
int ( "123" )
str ( 123 )
📌 ASCII
ord ( 'a' ) # 97
📌 Join
"" .join([ "a" , "b" , "c" ]) # abc
6️⃣ Queue (Deque)
from collections import deque
q = deque()
q.append( 1 ) # deque([1])
q.appendleft( 2 ) # deque([2,1])
q.pop() # 2 deque([1])
q.popleft() # 2 deque([])
7️⃣ Set
s = set ()
s.add( 1 )
1 in s
s.remove( 1 )
8️⃣ Dictionary (HashMap)
mp = {}
mp[ "a" ] = 1
📌 Access
mp[ "a" ]
"a" in mp
📌 Loop
for key in mp:
for val in mp.values():
for k, v in mp.items():
📌 Dict Comprehension
{i: i * 2 for i in range ( 3 )}
9️⃣ Tuple
t = ( 1 , 2 )
Immutable
Can be used as hashmap key
🔟 Heap (Priority Queue)
import heapq
minHeap = []
heapq.heappush(minHeap, 3 )
heapq.heappop(minHeap)
📌 Max Heap Trick
heapq.heappush(maxHeap, - val)
📌 Heapify
heapq.heapify(arr)
1️⃣1️⃣ Functions
def add (a, b):
return a + b
📌 Nested Functions
def outer ():
x = 10
def inner ():
return x
⚠️ Nonlocal
def outer ():
x = 1
def inner ():
nonlocal x
x += 1
1️⃣2️⃣ Classes
class MyClass :
def __init__ (self, nums):
self .nums = nums
def size (self):
return len ( self .nums)
🔥 Final Interview Tips
Use:
list → stack
deque → queue
set → fast lookup
dict → hashmap
heapq → priority queue