Python - 6

Python - 6: If Statements

if statements let your program make decisions based on whether a condition is true.

Your challenge: Check a number and a piece of text, then print a message only when each condition is true.

✦ Python Editor
▶ Output

Check a condition

An if statement begins with a condition: if age >= 18:

If that condition is true, Python runs the indented code underneath it.

Compare values

Use >= for “greater than or equal to” and == to check whether two values are equal.

For example: if favorite_food == "Pizza":

Indentation matters

The code inside an if statement must be indented. Python uses indentation to know which lines belong to the condition.

Try it: Change age to 15, then change favorite_food and see which messages appear.

Good Python habit: Use == when comparing values. A single = is used to assign a value to a variable.

👀 Show Example Solution
age = 18

if age >= 18:
    print("You are an adult")

favorite_food = "Pizza"

if favorite_food == "Pizza":
    print("Pizza is awesome!")

temperature = 75

if temperature > 70:
    print("It is warm outside")

Each message appears only when its condition evaluates to True.