Bookmarks

🟢 Easy

if / elif / else

What: if / elif / else is Python's decision fork — run one branch based on a condition, just like a SQL CASE WHEN. Why it matters: In pipelines every row passes a quality gate: if the amount is negative → reject, elif above threshold → flag for review, else → accept into the warehouse. How to remember: Python checks top to bottom and runs the first True branch, then skips the rest — order your conditions from most specific to most general.

Decision flow — if / elif / else
score = 85
if score ≥ 90 ? → A (no → next)
elif score ≥ 80 ? → B ✓ this fires
else → C (only if none above)
PySpark: F.when(col('score') >= 90,'A').when(col('score') >= 80,'B').otherwise('C')
Python
x = 0
if x % 2 == 0:
    print('even')
else:
    print('odd')

score = 85
if score >= 90:
    print('A')
elif score >= 80:
    print('B')
else:
    print('C')
Output
even
B
🟡 Intermediate

while — loop until condition false

What: A while loop repeats as long as its condition stays True — you control the counter (count), the condition (count <= 5), and the step (count += 1). Why it matters: Data waits are while loops: poll a landing folder until a file appears, retry a login up to 3 times, or sum from 1 to N — and a missing count += 1 creates an infinite loop that burns a cluster. How to remember: while = "keep going while this is true" — always make sure something inside the loop will eventually make it false.

while loop anatomy — counter + condition + step
init
count = 1
check
count <= 5 ?
yes → run / no → exit
body
print('praveen')
step
count += 1
↺ loop
⚠️ Missing count+=1 = infinite loop Retry pattern: attempts < maxattempts
Python
count = 1
while count <= 5:
    print('praveen')
    count += 1
print('i m done')

total, i = 0, 1
while i <= 5:
    total += i
    i += 1
print(total)  # 15

# Login retry — 3 attempts
correctpassword = 'admin123'
attempts, maxattempts = 0, 3
for entered in ['wrong','admin123']:
    if entered == correctpassword:
        print('password accepted'); break
    else:
        attempts+=1
        if attempts < maxattempts:
            print(f'sorry wrong, {maxattempts-attempts} left')
        else:
            print('max attempt reached')
Output
praveen
praveen
praveen
praveen
praveen
i m done
15
sorry wrong, 2 left
password accepted
🟢 Easy

for — iterate a known collection

What: A for loop iterates over a known collection — a list of fruits, each character in 'praveen', or numbers from range(5) — and hands you one item at a time. Why it matters: Every Spark for row in df.collect(), every preprocessing step, and every range()-driven partition walk is a for loop in disguise; unlike while it cannot overflow because the collection has a fixed end. How to remember: Read for f in fruits: as "for each fruit f inside fruits, do..." — the loop variable f becomes each element in order, no manual counter needed.

for in — what the variable holds each iteration
list
for f in ['apple','banana','mango']
applebananamango
f = each string in order
string
for c in 'praveen'
pra
c = each character
range
for i in range(5)
01234
i = 0→4 (stop before 5)
Python
fruits = ['apple','banana','mango','cherry']
for f in fruits:
    print(f)

for i in range(5):
    print(i, end=' ')  # 0 1 2 3 4
print()

for c in 'praveen':
    if c in 'aeiouAEIOU':
        print(c, end=' ')  # i a
Output
apple
banana
mango
cherry
0 1 2 3 4
i a
🟢 Easy

break / continue / pass

What: break exits the whole loop immediately, continue skips only the current iteration and keeps looping, and pass does nothing but keeps the syntax valid. Why it matters: In ETL you break when you have found the bad row and no longer need to scan, continue when you want to skip nulls but keep checking the rest, and pass when you outline an unimplemented handler without crashing. How to remember: Break = emergency exit, Continue = skip this seat, Pass = placeholder ticket.

break vs continue vs pass — over range(5)
break at i==3
0123 ✕4 skip
exit entire loop
continue at i==3
0123 skip4
skip one, keep looping
pass
def todo(): pass
valid placeholder — no error, no effect
Python
for i in range(5):
    if i==3: break
    print(i, end=' ')  # 0 1 2
print()

for i in range(5):
    if i==3: continue
    print(i, end=' ')  # 0 1 2 4
print()

def todo():
    pass  # placeholder — no error
print('pass did nothing')
Output
0 1 2
0 1 2 4
pass did nothing
🟡 Intermediate

input() & f-strings

What: input() reads a line as string from the user, and f"..." strings let you embed variables with {} — the modern way to format messages. Why it matters: Spark jobs log thousands of lines like f"loaded {row_count} rows from {path} in {elapsed}s"; mastering f-strings means readable logs without + str() gymnastics, and knowing input() always returns a string prevents silent type bugs in CLI tools. How to remember: The f means "fill in the braces" — Python evaluates everything inside {} and drops the result into the string.

f-string assembly — braces become values
f"my name is{name}""my name is praveen"
inside {} can be any expression: {i%2==0} {name.upper()} input() always returns string — cast with int()
Python
name = 'praveen'
print(f'my name is {name}')  # my name is praveen
print(f'welcome to icici, {name}')

for i in range(1,11):
    if i%2==0:
        print(f'{i} is even')
Output
my name is praveen
welcome to icici, praveen
2 is even
4 is even
6 is even
8 is even
10 is even