When you write a program, you often want it to make a choice. An if…else statement is Python’s way of asking a question and then deciding what to do based on the answer.
Think of it like this: “If it is raining, take an umbrella. Otherwise, wear sunglasses.” The program checks the condition and runs the matching instructions.
The basic idea
An if checks whether something is True. If it is, the program follows that path. If not, the program skips to the else part (if there is one).
age = 18
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")
Output:
You are an adult.
Adding more choices with elif
You can also add extra conditions using elif (short for “else if”).
temperature = 15
if temperature > 25:
print("It's hot.")
elif temperature > 15:
print("It's warm.")
else:
print("It's cold.")
Useful things to try
- Write a program that checks whether a number is positive or negative.
- Ask the user for their age and print whether they are a child, teenager, or adult.
- Create a program that prints whether today’s temperature is “hot”, “warm”, or “cold”.
Summary
ifruns a block of code if the condition is True.elseprovides an alternative when it’s False.eliflets you check several conditions in order.
With if…else, your programs can start making real decisions instead of just running straight through.





