control

Python Control Flow (If Statements)

Python Control Flow (If Statements)

Control flow structures allow your program to make decisions based on conditions. The if statement is used for this purpose.

If Statements

The if statement is used to test conditions. If the condition is True, the code inside the block runs:

      
        age = 18
        if age >= 18:
            print("You are an adult.")
        else:
            print("You are a minor.")
      
    

Activity

Try It Yourself!

Write a program that checks whether a number is even or odd.

Quick Quiz

Quick Quiz

  1. What is the purpose of the if statement in Python?
  2. What happens when the condition in an if statement is False?

Answers: The if statement checks a condition and runs code if the condition is True. If the condition is False, the code inside the else block will run (if defined).