Python Foundations·First steps

Control flow

Make decisions and repeat work with if, for and while.

Beginner 13 min read Lesson 2 of 480 Challenge included
What you will be able to do
  • Write if/elif/else chains
  • Loop over ranges and lists
  • Use break and continue

Control flow is how a program makes decisions and repeats work. Conditions are truthy or falsy; comparisons produce booleans.

control.pyPython
score = 78

if score >= 90:
    level = "excellent"
elif score >= 75:
    level = "passed"
else:
    level = "retry"

for day in range(1, 6):
    print(f"Study day {day}")

while score < 90:
    score += 2
  • break exits the loop immediately.
  • continue skips to the next iteration.
  • range(1, 6) yields 1 through 5 — the end is exclusive.
Your turn

FizzBuzz, briefly

Medium

Print numbers 1–15. For multiples of 3 print "Fizz", for 5 print "Buzz", for both print "FizzBuzz".

PYTHON · starter
for n in range(1, 16):
    # your code here
    pass
Check your understanding before moving on.
Knowledge checkQuestion 1

What does range(3) produce?

Select an answer to continue
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Next: Input and output
Lesson 2 of 480 · 0% of course