πŸ§‘‍🏫 Module 3: Python Control Flow: Mastering Conditions and Loops

Welcome to Module 3: Control Flow in Python! In this post, we’ll explore how to control the flow of your Python programs by making decisions with conditional statements and repeating tasks with loops. We’ll also cover some special tools like break, continue, and pass, as well as nested loops. These concepts are essential for making your code smarter, more efficient, and more interactive. Let’s dive in!

🧠 What is Control Flow in Python?

When you write a Python program, you’re essentially telling the computer what to do. But not all tasks are straightforward — sometimes, you need your code to make decisions based on certain conditions or repeat tasks until a goal is achieved. This is where control flow comes in.

Control flow allows you to:

  1. Make decisions with conditional statements like if, elif, and else.
  2. Repeat tasks with loops like for and while.
  3. Fine-tune loops with tools like break, continue, and pass.

Let’s start with conditional statements!

🟒 1. Conditional Statements: if, elif, else

Conditional statements allow your program to make decisions based on certain conditions. Think of it as a way to ask questions like, "Is it raining?" or "Is my age greater than 18?" Depending on the answer, your program will take different actions.

πŸ“‹ The Basic Structure of Conditional Statements:

python

if condition: # Code to run if the condition is true elif another_condition: # Code to run if the first condition is false and this one is true else: # Code to run if all conditions are false

πŸ“Œ Example: Checking if a Number is Positive, Negative, or Zero

python

number = int(input("Enter a number: ")) if number > 0: print("The number is positive.") elif number < 0: print("The number is negative.") else: print("The number is zero.")

πŸ” What’s Happening?

  • The if statement checks if the number is greater than 0.
  • If the condition is true, it prints "The number is positive."
  • If not, the program moves to the elif statement to check if the number is less than 0.
  • If all conditions are false, the else statement runs.
python

number = int(input("Enter a number: ")) if number % 2 == 0: print("The number is even.") else: print("The number is odd.")

🧩 2. Logical Operators and Boolean Expressions

To make more complex decisions, you can combine conditions using logical operators. These operators work with boolean values (True or False) to make your conditions smarter.

πŸ”§ Logical Operators:

OperatorDescriptionExample
andReturns True if both conditions are true(x > 0) and (x < 10)
orReturns True if at least one condition is true(x < 0) or (x > 10)
notReverses the boolean valuenot(x > 0)

πŸ“Œ Example: Checking Multiple Conditions

python

age = int(input("Enter your age: ")) if age > 18 and age < 60: print("You are eligible to work.") elif age >= 60: print("You are eligible for retirement.") else: print("You are too young to work.")

In this example:

  • The and operator ensures that both conditions are true before the message is printed.
  • The elif statement checks if the person is 60 or older.

πŸ” 3. Loops in Python (for loop, while loop)

Sometimes, you need to repeat a task multiple times. Instead of writing the same code over and over again, you can use a loop.

🧡 A Quick Introduction to Loops:

  1. for loop – Used when you know how many times you want to repeat something.
  2. while loop – Used when you want to repeat something until a condition is no longer true.

πŸ“Œ Example of a for Loop:

python

for i in range(5): print("Hello!")

What happens here?

  • The loop runs 5 times because we used range(5).
  • It prints "Hello!" each time the loop runs.

πŸ“Œ Example of a while Loop:

python

number = 1 while number <= 5: print(number) number += 1

What happens here?

  • The while loop runs as long as the condition (number <= 5) is true.
  • Each time it runs, it prints the value of number and then adds 1 to it.

4. Using break, continue, and pass Statements

Python provides some special keywords to control the flow of loops. These are:

  • break – Stops the loop entirely.
  • continue – Skips the rest of the code in the current loop iteration and moves to the next iteration.
  • pass – Does nothing (it’s just a placeholder).

πŸ“Œ Example of break:

python

for i in range(10): if i == 5: break print(i)

What happens here?

  • The loop stops when i becomes 5, and it doesn’t print any numbers after that.

πŸ“Œ Example of continue:

python

for i in range(10): if i == 5: continue print(i)

What happens here?

  • When i is 5, the continue statement skips the print statement and moves to the next iteration.

πŸ“Œ Example of pass:

python

for i in range(10): if i == 5: pass print(i)

What happens here?

  • The pass statement does nothing. It’s just a placeholder.

πŸŒ€ 5. Nested Loops and Conditional Statements

You can also have loops inside loops or conditions inside conditions. These are called nested loops and nested conditionals.

πŸ“Œ Example of a Nested Loop:

python

for i in range(3): for j in range(2): print(f"i = {i}, j = {j}")

What happens here?

  • The outer loop runs 3 times.
  • For each iteration of the outer loop, the inner loop runs 2 times.

The output looks like this:

i = 0, j = 0 i = 0, j = 1 i = 1, j = 0 i = 1, j = 1 i = 2, j = 0 i = 2, j = 1

🎯 Summary

In this lesson, we covered:

  1. Conditional Statements – Use if, elif, and else to make decisions in your code.
  2. Logical Operators – Combine conditions with and, or, and not.
  3. Loops – Use for and while loops to repeat tasks.
  4. Special Loop Controls – Use break, continue, and pass to control your loops.
  5. Nested Loops and Conditional Statements – Use loops and conditions inside each other to handle complex problems.

Comments