Control Flow Fundamentals

Lesson 3: Control Flow Fundamentals

🎯 Learning Objectives

By the end of this lesson, you will be able to:

  • Write conditional statements to control program flow
  • Use comparison and logical operators effectively
  • Handle basic input/output operations
  • Create simple interactive programs

📝 Introduction

Control flow is how we make our programs make decisions and repeat actions. Think of it as teaching your program to think and react!

1. Conditional Statements

If Statements

{"tool": "python-executor", "defaultValue": "temperature = 25\n\nif temperature > 30:\n print("It's hot!")\nelse:\n print("The temperature is fine.")"}

Loading tool...

If-Elif-Else Chains

{"tool": "python-executor", "defaultValue": "score = 85\n\nif score >= 90:\n grade = "A"\nelif score >= 80:\n grade = "B"\nelif score >= 70:\n grade = "C"\nelse:\n grade = "F"\n\nprint(f"Score: {score}, Grade: {grade}")"}

Loading tool...

2. Comparison Operators

Basic Comparisons

Loading tool...
Loading tool...

3. Logical Operators

AND, OR, NOT Operations

{"tool": "python-executor", "defaultValue": "is_sunny = True\nis_warm = True\n\nif is_sunny and is_warm:\n print("Perfect beach day!")\n\nif not is_sunny:\n print("Stay inside!")\n\nif is_sunny or is_warm:\n print("Decent weather!")"}

Loading tool...

4. Input/Output Operations

Basic Input

{"tool": "python-executor", "defaultValue": "# Note: In our playground, input is simulated\nname = input("What's your name? ")\nage = int(input("How old are you? "))\n\nprint(f"Hello, {name}! You are {age} years old.")\nprint(f"Next year you'll be {age + 1}!")"}

Formatted Output

{"tool": "python-executor", "defaultValue": "name = "Alice"\nage = 25\n\n# Different ways to format output\nprint(f"Hello, {name}! You are {age} years old.")\nprint("Hello, {}! You are {} years old.".format(name, age))\nprint("Hello, %s! You are %d years old." % (name, age))"}

🔨 Practice Exercises

Exercise 1: Temperature Converter

Loading tool...

Exercise 2: Login System

{"tool": "python-executor", "defaultValue": "# Create a simple login system\nusername = input("Username: ")\npassword = input("Password: ")\n\n# Add your login logic here\n# - Check if username is 'admin'\n# - Check if password is 'secret123'\n# - Print appropriate message"}

🎯 Challenge Project: Decision Tree Game

Loading tool...

✅ Knowledge Check

Loading tool...

📚 Additional Resources

  • Python documentation on control flow
  • Common patterns and best practices
  • Debugging tips for control flow issues
  • Interactive practice problems

💡 Pro Tips

  • Always consider all possible conditions
  • Use clear and descriptive condition names
  • Test edge cases in your conditions
  • Keep your logic simple and readable