Stop 01 · Programming & CS FoundationsTopics · 2/12

Control Flow

Part of the AI Engineer Roadmap

In short

Data is messy. Some rows are blank, some marks are negative, some labels are spelled wrong. Control flow is how your code decides what to do with each one instead of crashing.

if/elif/else, for and while loops, and comprehensions control the order code executes in — the backbone of every data-cleaning script and training loop you'll write.

The purple blob dressed as a traffic policeman waves one auto-rickshaw left and another right.

Think of it like this

The traffic police at a busy chowk

Each vehicle arrives and the policeman checks it. Ambulance? Let it through first. Truck? Send it to the outer road. Everyone else waits for the signal. if, elif and else are that policeman, and a loop is the endless line of vehicles.

Watch it happen

Grading marks = 72

marks = 72
marks >= 90?
A grade
marks >= 60?
B grade
C grade

1/4Python starts at the top of the if chain with marks set to 72.

The big ideas

What you need to know

Only one branch runs

In an if / elif / else chain, Python takes the first condition that is true and skips the rest. Order your checks from most specific to least.

for walks through things

for row in rows: runs once per item. Use range(5) when you just need to count 0 to 4.

while repeats until told to stop

while loss > 0.01: keeps going until the condition turns false. Forget to change the condition and it runs forever.

break and continue

break leaves the loop early. continue skips to the next item. Perfect for ignoring bad rows.

See it in code

Walk through the code

python
1marks = [72, 95, -4, 58, None, 88]2 3for m in marks:4    if m is None:5        print("missing, skipping")6        continue7    if m < 0:8        print("bad value, stopping")9        break10    if m >= 90:11        print(m, "A")12    elif m >= 60:13        print(m, "B")14    else:15        print(m, "C")

1/4The for loop takes each mark one at a time and names it m.

This prints 72 B, 95 A, then hits -4 and stops. The None, 58 and 88 never get checked because break ended the loop.

Save yourself the pain

Common mistakes

Oops

Writing if m >= 60 before if m >= 90, so a 95 gets a B.

Do this instead

Put the strictest condition first. Python stops at the first match.

Oops

Using == None to check for missing values.

Do this instead

Use is None. It's the Python way and avoids surprises with objects that redefine ==.

Your mission

Clean a list of temperatures

~15 min

Quick check

In a loop over rows, you want to ignore rows with a blank name but keep processing the rest. What do you use?

Go deeper

  1. 1Rewrite a few nested for-loops as list comprehensions to build the habit.
  2. 2Practice loop control (`break`, `continue`, `else` on loops) on small exercises.
  3. 3Trace through a training-loop skeleton and identify each control-flow construct.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge