This pre-read will:
- Explain how control flow shapes program logic
using if/else, loops, and functions.
- Show how Python makes decisions, performs
repetitive tasks, and structures code with modules.
- Teach how to combine conditional logic and loops
to solve real-world problems.
- Guide you toward writing organized, readable,
and reusable code through simple examples.
Part 1: The Big Picture – Why Does This
Matter?
- When you start programming, your code often
runs from top to bottom. But real-world problems aren’t always that
straightforward they need decisions, repetition, and organization.
- That’s where control flow and statements come
in. They give your programs the power to make choices, repeat tasks, and
organize logic efficiently.
Think of it like giving instructions to
a robot:
- “If the light is red, stop.”
- “Repeat this cleaning task 5 times.”
- “Call this helper whenever the floor is
dirty.”
- Without control flow, your robot (or your
code) would only follow one boring path — no decisions, no loops, no
flexibility.
After reading this, you’ll be able to:
- Understand how conditional statements (like if/else)
shape decisions in code.
- Use loops to perform tasks repeatedly without
rewriting the same lines.
- Organize logic using functions and modules for
cleaner, reusable code.
Where You’ll Use This:
- From automating tasks to analyzing data and
building applications, control flow is the brain behind all program
decisions.
Part 2: Your Roadmap Through This Topic
- Here’s what we’ll explore step-by-step:
Conditional Statements – How to make
decisions using if, elif, and else.
If/Else in Action – Writing
simple conditions and comparing values.
Loops – Repeating actions using for and
while.
Functions – Wrapping
logic into reusable blocks.
Modules – Grouping your
code into organized, sharable files.
The journey: Start with
basic choices, then build towards structuring entire workflows — the same way
you’d plan a day’s routine, from checking the weather to deciding what to wear.
Part 3: Key Terms to Listen For
- Term
- Condition A statement that can be True or
False
- If/Else Structure that controls what happens
when a condition is met or not
- Loop Repeats a block of code until a condition
changes
- Function A reusable block of code that
performs one task
- Module A file containing Python code you can
import and reuse
- These terms form the language of logic in
Python. Once you understand them, every piece of code becomes more
predictable and powerful.
Part 4: Concepts in Action
- Let’s dive into each concept one by one —
short, simple, and practical.
1. Understanding Conditional
Statements
- Conditional statements help your program make
decisions. They check conditions using if, elif, and else statements and
run specific code based on whether something is true or false.
Example:
age = 18
if age >= 18:
print("You can vote!")
else:
print("Sorry, you are not eligible.")
Here:
- If the condition age >= 18 is True, Python
runs the first block.
- Otherwise, it runs the else block.
- Tip: Conditions often use comparison operators
like ==, !=, <, >, <=, >=.
- Think of this as: a traffic light for your
code — “If red, stop; if green, go.”
Why it matters:
- Conditions are the building blocks of
decision-making. They help your code adapt and react, just like humans do
when making choices.
2. Combining Conditions
- You can combine multiple conditions using
logical operators:
and → All conditions must be true
or → At least one condition must be
true
not → Reverses the result
Example:
temperature = 28
humidity = 70
if temperature > 25 and humidity
> 60:
print("It’s hot and humid today!")
3. Loops: Automating Repetition
- Loops allow you to repeat tasks without
writing code again and again.
The for Loop
- Used when you know how many times you want to
repeat something.
for i in range(5):
print("Iteration:", i)
- This will print numbers from 0 to 4.
The while Loop
- Used when you want to repeat something until a
condition changes.
count = 0
while count < 3:
print("Count:", count)
count += 1
- Tip: Be careful! A while loop runs until its
condition becomes False. If it never does, your code may loop forever.
4. Breaking and Skipping in Loops
Example:
for num in range(1, 6):
if num == 3:
continue # skips number 3
if num == 5:
break # stops the loop
print(num)
5. Functions: Reusable Building
Blocks
- Functions allow you to group a block of code
and use it whenever needed.
- They make your code organized, reusable, and
easy to test.
Example:
def greet(name):
return f"Hello, {name}!"
print(greet("Amit"))
output
Hello, Amit!
- Here:
- def defines the function.
- name is a parameter.
- greet("Amit") calls the function.
Where you’ll use it:
- Functions help keep your code DRY — Don’t
Repeat Yourself. Instead of writing the same logic in multiple places, you
write once and reuse it.
Bonus Tip:
- Functions can even call other functions — this
helps break big problems into smaller, easy-to-manage tasks
6. Passing Data and Returning
Results
- Functions can also return values using the
return keyword.
def add(a, b):
return a + b
result = add(5, 3)
print("Sum:", result)
- Returning values helps your code pass data
between functions and main logic.
7. Organizing Code with Modules
- When your code grows larger, you don’t want
everything in one file.
- That’s where modules come in — they let you
split your program into manageable files.
- You can import Python’s built-in modules or
your own custom ones.
Example:
- Create a file called helpers.py:
def add(a, b):
return a + b
- Import and use it in another file:
import helpers
print(helpers.add(5, 3))
Why it matters:
- Modules keep your code tidy, just like folders
keep your documents organized. They also let you share your logic across
projects.
Real-world use:
- Python’s built-in modules like math, datetime,
and os are used every day for calculations, time management, and file
operations.
8. Real-Life Analogy
Think of programming like running a
restaurant:
- Conditional statements decide what to cook (if
vegetarian → make salad; else → make pasta).
- Loops help prepare multiple dishes (cook 10
plates of pasta).
- Functions are like kitchen stations — salad
section, grill section — each does one specific task.
- Modules are entire kitchens working together
(dessert module, main course module).
- By mastering control flow, you’re learning how
to make your “kitchen” efficient and logical.
Part 5. Common Mistakes to Avoid
- Forgetting colons (:) at the end of if, elif,
or loop lines.
- Misusing indentation — Python depends on it to
understand structure.
- Writing infinite loops without an exit
condition.
- Using = instead of == in condition checks.
10. Putting It All Together
Here’s a small example that combines
everything you’ve learned:
def check_grade(score):
if score >= 90:
return "Excellent"
elif score >= 75:
return "Good"
elif score >= 50:
return "Average"
else:
return "Needs Improvement"
scores = [95, 82, 47, 60]
for s in scores:
print(f"Score: {s}, Grade: {check_grade(s)}")
This example uses:
- Conditionals to decide grades,
- Functions to make the logic reusable,
- Loops to process multiple scores.
Part 6: Practice & Reflection
(Optional)
Try this mini challenge:
Challenge: Write a program that takes a number as input and:
- Prints “Fizz” if divisible by 3
- Prints “Buzz” if divisible by 5
- Prints “FizzBuzz” if divisible by both
- Otherwise prints the number itself
Hint: Combine if, elif, and else with
modulo (%) operator.
Think about:
- How could you make this more efficient?
- Can you put the logic in a function and reuse
it?
Reflect:
- Once you complete this, you’ll realize —
control flow isn’t just a Python concept.
- It’s the logic of thinking that powers every
intelligent system.
Part 7: Consolidation: Key Takeaways
& Next Steps
Key Takeaways
- Control flow = program logic.
- If/Else = decision-making.
- Loops = repetition.
- Functions = reuse and structure.
- Modules = organization and scaling
Next Steps
- To prepare for the lecture:
- Try writing simple if and for examples in your
IDE.
- Modify existing examples to include break,
continue, and nested conditions.
- Create one simple script that reads a number
and prints whether it’s even or odd using conditionals and loops.