Every decision follows the same pattern: if one thing is true, do this; otherwise, do that. Traffic lights pick a colour; thermostats decide whether to heat the room. Python programmes make decisions with exactly the same logic, using the keywords if, elif, and else. Master this trio and your code starts to feel genuinely intelligent.

What is a simple if statement?

An if statement runs a block of code only when a condition is True. If the condition is False, the block is skipped entirely.

temperature = 28

if temperature > 25:
    print("It is hot today.")

print("This line always runs.")

Output (because 28 > 25 is True):

It is hot today.
This line always runs.

The condition (temperature > 25) is a Boolean expression — it evaluates to either True or False. The indented block only executes when the result is True.

How do you add an else branch?

An else branch runs when the if condition is False — it is the "otherwise" option.

score = 45

if score >= 50:
    print("You passed!")
else:
    print("You did not pass. Keep practising.")

Exactly one of the two blocks will run, depending on the value of score. There is no situation where both run, and no situation where neither runs.

How do you handle multiple conditions with elif?

elif stands for "else if" and lets you check a series of conditions in order. Python tests each one in turn and runs the first block whose condition is True, then skips all remaining branches.

score = int(input("Enter your score: "))

if score >= 90:
    print("Grade: A*")
elif score >= 80:
    print("Grade: A")
elif score >= 70:
    print("Grade: B")
elif score >= 60:
    print("Grade: C")
else:
    print("Grade: below C — keep revising!")

Trace with score = 75:

Condition checked Result Action
75 >= 90 False Skip
75 >= 80 False Skip
75 >= 70 True Print "Grade: B" — stop checking

You can have as many elif branches as you need. The else at the end is optional — it acts as a catch-all for anything not covered by the conditions above it.

What comparison operators can you use in conditions?

Operator Meaning Example Result
== Equal to 5 == 5 True
!= Not equal to 5 != 3 True
> Greater than 7 > 4 True
< Less than 3 < 2 False
>= Greater than or equal to 5 >= 5 True
<= Less than or equal to 4 <= 3 False

Do not confuse = (assignment, stores a value) with == (comparison, tests equality). This is one of the most common beginner errors.

How do you combine conditions with and / or / not?

Use Python's logical operators to combine multiple conditions:

age      = 15
has_card = True

if age >= 16 or has_card:
    print("You may enter the exhibition.")
else:
    print("Entry not permitted.")
Operator Meaning True when …
and Both must be true Both conditions are True
or At least one must be true Either or both conditions are True
not Inverts the result The condition is False

What does a nested if statement look like?

A nested if places one decision inside another. Use it when a second condition only makes sense if the first was already True.

username = input("Username: ")
password = input("Password: ")

if username == "admin":
    if password == "secure123":
        print("Access granted.")
    else:
        print("Wrong password.")
else:
    print("Username not recognised.")

Keep nesting to a maximum of two or three levels — deeper nesting becomes very hard to read and debug.

What are the most common mistakes with if-else in Python?

Mistake Example Fix
Using = instead of == if age = 18: Use == for comparison
Missing the colon if score > 50 Add : after the condition
Wrong indentation Code inside if not indented Indent 4 spaces consistently
Checking the same condition in multiple elif branches Can cause unreachable code Order conditions from most specific to least

Frequently asked questions

Does Python have a switch-case statement like other languages?

Python 3.10 introduced match-case statements, which serve a similar purpose to switch-case in Java or C. However, for KS3 and most GCSE work, if-elif-else chains are the expected approach and are perfectly capable of handling any decision scenario.

Can I put a for loop inside an if statement?

Yes. Any valid Python code can appear inside an if block, including loops, function calls, and further if statements. The indentation levels simply nest further inward.

What happens if two elif conditions are both True?

Only the first True branch runs. Python tests conditions from top to bottom and stops at the first match. The order of your elif branches therefore matters — put the most specific or most restrictive conditions first.

How do I test user input with an if-else statement?

Always convert input to the correct type first. input() always returns a string, so score = int(input("Score: ")) before comparing score >= 70. Comparing a string to an integer in Python does not raise an error but will always evaluate to False, which is a silent and confusing bug.


Want to practise writing Python if-elif-else chains with step-by-step guidance? Visit aitutors.me and ask Professor Turing to set you a decision-making challenge.