Control flow in Python refers to the order in which statements are executed in a program. It helps you make decisions, repeat tasks, and control how your code runs.
Here’s a clear and beginner-friendly explanation:
✅ 1. Conditional Statements (Decision Making)
Used to run specific code based on conditions.
if statement
x = 10
if x > 5:
print("x is greater than 5")
if-else statement
age = 17
if age >= 18:
print("You can vote")
else:
print("You cannot vote")
if-elif-else
marks = 85
if marks >= 90:
print("Grade A")
elif marks >= 75:
print("Grade B")
else:
print("Grade C")
✅ 2. Loops (Repeating Tasks)
➡ for loop
Used to iterate over sequences (list, string, range, etc.)
Example:
for i in range(5):
print(i)
Looping through a list:
fruits = ["apple", "banana", "mango"]
for fruit in fruits:
print(fruit)
➡ while loop
Runs as long as a condition is true.
count = 1
while count <= 5:
print(count)
count += 1
✅ 3. Loop Control Statements
break → stop the loop immediately
for i in range(10):
if i == 5:
break
print(i)
continue → skip the current iteration
for i in range(5):
if i == 2:
continue
print(i)
pass → placeholder, does nothing
for i in range(5):
pass # future code can go here
🎯 Summary Table
| Concept | Use |
|---|---|
if |
Check a condition |
if-else |
Decision making |
elif |
Multiple conditions |
for |
Loop through sequences |
while |
Loop until condition false |
break |
Stop loop |
continue |
Skip iteration |
pass |
Do nothing (placeholder) |
Here’s more detailed and advanced concepts of Control Flow in Python, including examples, diagrams, and practice problems.
⭐ 1. Nested Control Flow
You can put one control structure inside another.
Example: Nested if
age = 20
citizen = True
if age >= 18:
if citizen:
print("Eligible to vote")
else:
print("Not a citizen")
else:
print("Underage")
Example: Nested Loops
for i in range(3):
for j in range(2):
print(i, j)
⭐ 2. Loop with else
Python has a unique feature:
for…else
The else block executes only if the loop completes normally (without a break).
for num in [1, 2, 3]:
print(num)
else:
print("Loop finished")
Useful Example:
Search for an element:
nums = [10, 20, 30, 40]
for n in nums:
if n == 25:
print("Found!")
break
else:
print("Not found!")
⭐ 3. while…else
Same concept as for…else.
count = 1
while count <= 3:
print(count)
count += 1
else:
print("Loop ended normally")
⭐ 4. Range Variations
range() can take 1, 2, or 3 arguments.
Example:
range(5) # 0 to 4
range(2, 6) # 2 to 5
range(1, 10, 2) # 1, 3, 5, 7, 9
⭐ 5. Infinite Loops
A loop that never ends unless you break it.
while True:
name = input("Enter name (or 'stop'): ")
if name == "stop":
break
⭐ 6. Logical Operators in Control Flow
| Operator | Meaning |
|---|---|
and |
True if both conditions are true |
or |
True if any condition is true |
not |
Reverses the condition |
Example:
age = 20
has_license = True
if age >= 18 and has_license:
print("You can drive")
⭐ 7. Using break and continue in loops
break example:
for i in range(10):
if i == 4:
break
print(i)
continue example:
for i in range(5):
if i == 3:
continue
print(i)
⭐ 8. pass Statement
pass is used as a placeholder.
if True:
pass # TODO: Write logic later
⭐ 9. Control Flow with Match Case (Python 3.10+)
Like switch-case in other languages.
status = 2
match status:
case 1:
print("Start")
case 2:
print("Processing")
case 3:
print("Completed")
case _:
print("Unknown")
⭐ Practice Questions (with increasing difficulty)
Level 1 (Basic)
- Write a program to check if a number is positive, negative, or zero.
- Print numbers from 1 to 10 using a loop.
- Print even numbers from 1 to 20.
Level 2 (Intermediate)
- Write a program to find the sum of digits of a number.
- Use a while loop to print a multiplication table of any number.
- Use a for-else loop to search an element in a list.
Level 3 (Advanced)
- Write a program to check if a number is a prime number.
- Use nested loops to print a pattern:
*
**
***
****
- Use match-case to display menu choices (1–Start, 2–Stop, etc.)
Would you like:
📘 Notes (PDF style)
🎥 Visual diagrams
📝 Solved practice problems
📚 MCQs for exam preparation
Just tell me what you need!
.jpg)
0 Comments