Back to website
Mayank Aggarwal Learning Center

Phase 02 · Python FoundationsLesson 3 of 8

Conditions & Loops

Make decisions with if/elif/else and repeat work with for and while loops.

  • 17:08 video
  • 3 min read
  • Beginner
01

The problem

So far your programs run straight down, line by line. Real programs need to make decisions ("is this user logged in?") and repeat work ("process every row in this file"). That's what control flow gives you: conditions and loops.

02

The concept

Making decisions with if#

Python
score = 72

if score >= 90:
    grade = "A"
elif score >= 70:
    grade = "B"
else:
    grade = "C"

print(grade)  # B
  • Python checks each condition top to bottom and runs the first block that's true.
  • elif ("else if") adds more checks; else catches everything left over.
  • Indentation defines the block. Four spaces is the standard.

How Python picks a block

  1. score = 72
  2. Check conditions in order
  • score >= 90
    1. grade = "A"
  • elif score >= 70
    1. grade = "B"runs here, so the result is B
  • else
    1. grade = "C"catches everything left over
Conditions are checked top to bottom and only the first true block runs.

You can combine conditions with and, or and not:

Python
if age >= 18 and has_id:
    print("Entry allowed")

Repeating with for#

A for loop runs once for each item in a sequence:

Python
tools = ["Python", "SQL", "Git"]
for tool in tools:
    print(f"Learning {tool}")

To repeat something a set number of times, use range():

Python
for i in range(1, 6):
    print(i)   # 1 2 3 4 5   (the stop value 6 is excluded)

range(0, 10, 2)  # 0, 2, 4, 6, 8

Repeating with while#

A while loop repeats as long as a condition stays true. Use it when you don't know in advance how many repetitions you'll need:

Python
attempts = 0
while attempts < 3:
    print("Trying to connect…")
    attempts += 1

A while loop

  1. Check attempts < 3repeat only while it stays true
  2. Print "Trying to connect…"
  3. attempts += 1moves the loop toward stopping

Stops once attempts < 3 is false

Controlling loops: break and continue#

Python
for n in range(1, 10):
    if n == 3:
        continue   # skip 3, go straight to the next number
    if n == 6:
        break      # stop the loop completely
    print(n)       # prints 1 2 4 5
  • break exits the loop immediately.
  • continue skips the rest of the current iteration.
  • pass does nothing; it's a placeholder when Python requires a block but you have nothing to write yet.

Putting it together: FizzBuzz#

A classic interview warm-up that combines both ideas:

Python
for n in range(1, 21):
    if n % 3 == 0 and n % 5 == 0:
        print("FizzBuzz")
    elif n % 3 == 0:
        print("Fizz")
    elif n % 5 == 0:
        print("Buzz")
    else:
        print(n)

Notice the order: the "both" case must come first, otherwise 15 would match "divisible by 3" and print "Fizz". Order of conditions is a common source of subtle bugs.

03

Try it

Your task · about 10 minutes

Write FizzBuzz for 1–20: print "Fizz" for multiples of 3, "Buzz" for multiples of 5, "FizzBuzz" for both, otherwise the number.

04

Recap & next

Key takeaways

  1. if, elif and else choose which block runs; indentation defines each block.
  2. for loops iterate over any sequence: lists, strings, ranges.
  3. range(start, stop, step) generates numbers up to, but not including, stop.
  4. while repeats as long as its condition stays true. Make sure the condition eventually becomes false.
  5. break exits a loop immediately; continue skips to the next iteration.

Quick check

2 questions · instant feedback

1Which numbers does range(1, 5) produce?
2What does continue do inside a loop?

Finished this lesson?

Mark it complete to track your progress and keep your streak going.

Next