Control Flow

This lesson focuses on control flow in Python, which allows you to make decisions and repeat actions in your code. You'll learn about conditional statements (if, elif, else) and loops (for, while) that are fundamental to programming and data science.

Learning Objectives

  • Understand and use `if`, `elif`, and `else` statements for conditional execution.
  • Implement `for` loops to iterate through sequences (lists, strings, etc.).
  • Utilize `while` loops for repetitive tasks based on conditions.
  • Apply control flow techniques to solve basic programming problems.

Text-to-Speech

Listen to the lesson content

Lesson Content

Introduction to Control Flow

Control flow determines the order in which your code is executed. It allows your program to make decisions and repeat actions based on certain conditions. Without control flow, programs would execute linearly, one line after another, which is very limited. Python provides two main types of control flow: conditional statements and loops.

Conditional Statements: if, elif, else

Conditional statements allow you to execute different blocks of code based on whether a condition is true or false.

  • if statement: Executes a block of code if a condition is true.
    python age = 20 if age >= 18: print("You are an adult.")
  • elif (else if) statement: Checks another condition if the previous if or elif conditions are false.
    python score = 75 if score >= 90: print("Grade: A") elif score >= 80: print("Grade: B") elif score >= 70: print("Grade: C")
  • else statement: Executes a block of code if all preceding if and elif conditions are false.
    python temperature = 10 if temperature > 25: print("It's hot!") elif temperature > 10: print("It's mild.") else: print("It's cold!")

Loops: for Loops

Loops allow you to repeat a block of code multiple times. for loops are used to iterate over a sequence (e.g., a list, a string, or a range of numbers).

  • Iterating through a list:
    python fruits = ["apple", "banana", "cherry"] for fruit in fruits: print(fruit)
  • Iterating through a string:
    python word = "Python" for letter in word: print(letter)
  • Using range(): The range() function generates a sequence of numbers.
    python for i in range(5): # Generates numbers from 0 to 4 print(i)

Loops: while Loops

while loops repeat a block of code as long as a condition is true. Be careful to avoid infinite loops by ensuring the condition eventually becomes false.

```python
count = 0
while count < 3:
    print("Count:", count)
    count += 1  # Increment the counter;  crucial to avoid an infinite loop!
```

Loop Control Statements: break and continue

Sometimes you need more control inside your loops.
* break: Exits the loop entirely.
python numbers = [1, 2, 3, 4, 5] for number in numbers: if number == 3: break print(number) # Output: 1, 2
* continue: Skips the rest of the current iteration and goes to the next.
python for number in range(1, 6): if number == 3: continue print(number) # Output: 1, 2, 4, 5

Progress
0%