Pre-Read Notes: Control Flow & Functions in Practice
1. What is Control Flow?
- Control flow decides how and when different parts of your code
run.
- It helps your program:
- Make decisions (if/else)
- Do repetitive tasks (loops)
- Stay organized (functions
and modules)
💡 Think: “If it rains, take an umbrella; else, wear
sunglasses.”
That’s control flow
in real life!
2. Conditional Statements (if, elif,
else)
Used to make
decisions based on conditions.
Example:
age = 18
if age >= 18:
print("You
can vote!")
else:
print("Not
eligible")
🧠 Tip:
Use comparison
operators like ==, !=, <, >, <=, >=.
3. Combining Conditions
Use logical operators:
- and → both conditions must
be true
- or → any one condition is true
- not → reverses the condition
Example:
if temp > 25 and
humidity > 60:
print("Hot
and humid day!")
4. Loops
Loops help run
code multiple times.
for loop → When you know
how many times to repeat
for i in range(5):
print(i)
while loop → When you
repeat until a condition changes
count = 0
while
count < 3:
print(count)
count += 1
5. Controlling Loops
- break → stop the loop early
- continue → skip current step, go to next
Example:
for num in range(1,6):
if num
== 3:
continue
if num
== 5:
break
print(num)
6. Functions
Functions group code
you can reuse anytime.
Example:
def greet(name):
return f"Hello,
{name}!"
print(greet("Amit"))
✅ Keeps your code clean, short, and easy to debug.
7. Modules
Modules are separate
Python files that store reusable code.
Example:
File: helpers.py
def add(a, b):
return a + b
Use it:
import
helpers
print(helpers.add(5,3))
8. Common Mistakes
❌ Forgetting : after if, for, or while
❌ Wrong indentation (Python is space-sensitive)
❌ Using = instead of ==
❌ Infinite loops (condition never becomes False)
9. Quick Practice – FizzBuzz
Challenge:
Write a program that:
- Prints “Fizz” if divisible by 3
- Prints “Buzz” if divisible by 5
- Prints “FizzBuzz” if divisible by both
- Otherwise prints the number
💡 Use if, elif, and % operator.
10. Key Takeaways
|
Concept |
Meaning |
|
If/Else |
Makes decisions |
|
Loops |
Repeat actions |
|
Functions |
Reuse code |
|
Modules |
Organize big
programs |
|
Control Flow |
Brain of your
program |