🔁 Python Basics

Control Flow: if & Loops

नियंत्रण प्रवाह: if और लूप

⏱ 4 hr4 topicsInteractive
🎯 By the end: You can write conditional branches and both kinds of loop, use break and continue correctly, and explain why indentation defines a block in Python.

So far our programs run straight down, line by line. Control flow changes that: it lets a program make decisions (do this, not that) and repeat work (do this 100 times). These two ideas — selection and iteration — are the heart of all programming. And in Python, one thing controls it all: indentation.

1Making decisions: if, elif, else

An if statement runs a block of code only when a condition is True. Add elif (else-if) for more conditions, and else for everything that's left:

if marks >= 90:
    print("Grade A")
elif marks >= 60:
    print("Grade B")
else:
    print("Grade C")
Indentation IS the block in Python. Unlike many languages that use { } braces, Python uses indentation (normally 4 spaces) to show which lines belong inside the if. Every line of a block must be indented the same amount, and the condition line must end with a colon :. Get the indentation wrong and you get an error or wrong behaviour.

Run the playground and change marks to 95, then 70, then 40 — watch which branch runs each time.

if / elif / else▶ runs real Python
main.py
Output
Click Run to execute.
Key points
  • if runs a block when its condition is True; elif adds more conditions; else catches the rest.
  • The condition line ends with a colon :.
  • Python uses indentation (usually 4 spaces), not braces, to define a block — it must be consistent.

2Repeating with for and range()

A for loop repeats a block a known number of times. It's almost always paired with range(), which generates a sequence of numbers:

  • range(5) → 0, 1, 2, 3, 4 (starts at 0, stops before 5).
  • range(1, 6) → 1, 2, 3, 4, 5 (start, stop).
  • range(0, 10, 2) → 0, 2, 4, 6, 8 (start, stop, step).
The big gotcha: range(start, stop) stops just before stop — the stop value is never included. So range(1, 6) gives 1 to 5, not 1 to 6.

The playground below prints a multiplication table — change the number 5 to build any table you like:

for loop: a multiplication table▶ runs real Python
main.py
Output
Click Run to execute.
Key points
  • A for loop repeats a block a known number of times, usually with range().
  • range(start, stop, step); the stop value is excluded.
  • range(1, 6) gives 1,2,3,4,5 — not 6.

3while loops, break and continue

A while loop repeats as long as a condition stays True — use it when you don't know in advance how many times to loop:

n = 1
while n <= 5:
    print(n)
    n += 1   # MUST change n, or the loop never ends
Infinite loop danger: a while loop needs something inside it that eventually makes the condition False (here, n += 1). Forget it and the loop runs forever. (Our playground stops after 10 seconds to protect you.)

Controlling a loop

  • break — exit the loop immediately, right now.
  • continue — skip the rest of this turn and jump to the next iteration.

Loops can also be nested (a loop inside a loop) — useful for grids and tables. Try the playground:

while, break & continue▶ runs real Python
main.py
Output
Click Run to execute.
Key points
  • A while loop repeats as long as its condition is True; something inside must eventually make it False.
  • break exits the loop immediately; continue skips to the next iteration.
  • Loops can be nested (a loop inside a loop) for grids and tables.

4Watch a loop run, step by step

It's one thing to run a loop and see its output; it's another to watch the loop variable change on every pass. Use the visualizer below: drag the start, stop and step sliders, then press Step to advance one iteration at a time (or Run all). Watch i update and the output grow — and notice again that the loop stops before the stop value.

Loop Visualizer — step through range()
for i in range(0, 5, 1):
    print(i)
i =
iteration 0
Output
Key points
  • On each iteration the loop variable i takes the next value from range().
  • The loop ends when range() is exhausted — the stop value is never reached.
  • Stepping through one iteration at a time makes the flow of a loop concrete.

★ Project: multiplication table generator

Using the playground, build a small program that:

  1. Stores a number in a variable (e.g. num = 7).
  2. Uses a for loop with range(1, 11) to print its table from 1 to 10.
  3. Then add an if inside the loop that prints 'halfway!' when i is 5.
  4. Bonus: use a while loop to do the same table, and make sure it isn't infinite.

Ready for the chapter test?

Answer 5 questions. Score 60% or more to mark this chapter complete.

Start the test →

💡 Log in to save your progress and earn the certificate.