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.
F.when(col('score') >= 90,'A').when(col('score') >= 80,'B').otherwise('C')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')even
Bwhile — 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.
count+=1 = infinite loop
Retry pattern: attempts < maxattempts
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')praveen
praveen
praveen
praveen
praveen
i m done
15
sorry wrong, 2 left
password acceptedfor — 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.
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 aapple
banana
mango
cherry
0 1 2 3 4
i abreak / 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.
def todo(): passfor 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')0 1 2
0 1 2 4
pass did nothinginput() & 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.
{} can be any expression: {i%2==0} {name.upper()}
input() always returns string — cast with int()
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')my name is praveen
welcome to icici, praveen
2 is even
4 is even
6 is even
8 is even
10 is even