Flow of Control in Python — Lesson
1) Hook — The Traffic Signal and Your Python Code
Imagine you are crossing a busy street in Mumbai. The traffic signal controls when you walk, stop, or wait. Similarly, in Python programming, flow of control decides which part of the code runs and when. Just like you don’t cross on a red light, Python doesn’t execute every line sequentially—it follows conditions and loops to control the “traffic” of your program.
2) Core Concepts — Understanding Flow of Control in Python
Python provides three main ways to control the flow of your program:
- Conditional Statements — Make decisions using
if,if-else, andif-elif-else. - Loops — Repeat actions using
forandwhileloops. - Control Statements — Modify loop behavior with
break,continue, andpass.
Conditional Statements
They allow your program to take decisions based on conditions.
| Syntax | Description | Example |
|---|---|---|
|
if condition: statement(s) |
Executes statements if condition is True. |
if marks >= 33: |
|
if condition: statement(s) else: statement(s) |
Executes one block if condition is True, else executes another. |
if age >= 18: |
|
if condition1: statement(s) elif condition2: statement(s) else: statement(s) |
Checks multiple conditions in sequence. |
if marks >= 90: |
Loops
Loops help repeat a block of code multiple times.
| Loop Type | Syntax | Example |
|---|---|---|
| for loop |
for variable in sequence: statement(s) |
for i in range(1,6): |
| while loop |
while condition: statement(s) |
count = 1 |
Control Statements in Loops
- break: Exits the loop immediately.
- continue: Skips the current iteration and continues with the next.
- pass: Does nothing; used as a placeholder.
Example:
for i in range(1, 6):
if i == 3:
break # Loop stops when i is 3
print(i)
3) Key Formulas/Rules
Rule 1: Indentation is mandatory in Python to define blocks under control statements.
Rule 2: if conditions must evaluate to True or False.
Rule 3: Use elif to check multiple conditions sequentially.
Rule 4: for loops iterate over a sequence like list, tuple, or range().
Rule 5: Use break to exit loops early and continue to skip to next iteration.
4) Did You Know?
Python was named after the British comedy group Monty Python — not the snake! Its creator, Guido van Rossum, wanted a name that was short, unique, and slightly mysterious. Just like Monty Python’s sketches, Python code is designed to be fun and readable.
5) Exam Tips — Common Mistakes & Board Exam Patterns
- Indentation Errors: Always indent blocks under
if,for,while, andelse. Missing or inconsistent indentation leads to errors. - Colon (:): Don’t forget the colon at the end of control statements (e.g.,
if condition:). - Logical Conditions: Use correct operators like
==for comparison, not=(assignment). - Loop Boundaries: Remember
range(1,6)runs from 1 to 5, not 6. - Practice Writing: Previous CBSE questions often ask for writing programs using
if-elseand loops, or to trace output of given code.
Previous Year Question Pattern Examples:
- Write a Python program to check if a student has passed or failed based on marks.
- Trace the output of a loop that prints numbers from 1 to 10 but skips multiples of 3.
- Write a program using
if-elif-elseto assign grades based on marks.
Flow of Control in Python — Mcq
Flow of Control in Python — Mnemonic
Mnemonic 1: FLOW Control in Python – "IF, ELSE, LOOP, GO!" 🚦
- I – IF condition checks ✅
- E – ELSE gives the alternative path 🔄
- L – LOOP repeats (for/while) 🔁
- G – GO to next statement after loop or condition ▶️
Remember: "If Else Loop Go, Python ka flow!" 😄
Mnemonic 2: Hindi Rhyming Trick for Flow Control 🐍
"Agar (If) socho, warna (Else) bolo, Jab tak (While) chale, tab tak (For) golo!"
- Agar (If) – condition check karna
- Warna (Else) – jab condition false ho
- Jab tak (While) – repeat jab tak condition true
- Tab tak (For) – fixed number baar repeat karna
Fun and easy to recall during exams! 😎
Mnemonic 3: Acronym "IFELOOP" for Python Control Flow 🔤
- I – IF (condition check)
- F – FOR loop (fixed iteration)
- E – ELSE (alternate path)
- L – LOOP (general loops)
- O – OUTPUT after control flow
- O – OPTIONAL else-if (
elif) - P – PASS statement (do nothing)
Think: "I Feel Else Loop On Python!" 🐍
Mission: Master This Topic!
Reinforce what you learned with fun activities
Ready to Battle? Test Your Knowledge!
Practice MCQs, build combos, climb the leaderboard!
Start Practice