if — Easy Examples
Starts a conditional statement; executes block only if condition is true
Basic conditional
Using if to make decisions.
python
temperature = 35 if temperature > 30: print("It's hot!") x = 10 if x > 0: print("Positive") elif x == 0: print("Zero") else: print("Negative")
Expected Output
It's hot! Positive
if evaluates a condition. The indented block runs only when the condition is True.
If with user input checking
Validating values with conditionals.
python
age = 17 if age >= 18: status = "adult" elif age >= 13: status = "teenager" else: status = "child" print(f"Age {age}: {status}") # Checking membership fruits = ["apple", "banana", "cherry"] item = "banana" if item in fruits: print(f"{item} is in the list")
Expected Output
Age 17: teenager banana is in the list
if works with any expression that evaluates to True or False, including comparisons, membership tests, and truthiness checks.
Want to try these examples interactively?
Open Easy Playground