Python Conditional Statements — Beginner's Guide
if, elif, else — rules, syntax, and usage with clear examples
■ What are Conditional Statements?
Conditional statements let your program make decisions. Based on whether a condition is True or False, Python runs different blocks of code. This is
how programs think and respond differently to different situations.
Python has three conditional keywords: if, elif, and else.
■ Comparison Operators — Used Inside Conditions
Every condition uses a comparison operator to produce True or False.
Operator Meaning Example Result
== Equal to 5 == 5 True
!= Not equal to 5 != 3 True
> Greater than 7 > 4 True
< Less than 3 < 10 True
>= Greater than or equal to 5 >= 5 True
<= Less than or equal to 4 <= 6 True
■ = is assignment (x = 5). == is comparison (x == 5). Never confuse them inside conditions.
■ Logical Operators — Combining Conditions
Use these to combine two or more conditions into one.
Operator Meaning Example Result
and Both conditions must be True 5>3 and 10>7 True
or At least one must be True 5>3 or 2>9 True
not Reverses True to False or vice versa not(5>3) False
01 if Statement
The most basic conditional. The code block inside runs only if the condition is True. If the condition is False, Python skips it entirely.
Syntax:
if condition:
# code runs only if condition is True
Rules:
1. The condition is followed by a colon :
2. The code block inside must be indented (4 spaces or 1 Tab).
3. Indentation is NOT optional in Python — it defines the block.
Example 1 — simple check:
age = 20
if age >= 18:
print('You are an adult.')
>> You are an adult.
Example 2 — condition is False, nothing prints:
age = 15
if age >= 18:
print('You are an adult.')
# No output — condition was False, block was skipped
Example 3 — checking a string:
name = 'Zia'
if name == 'Zia':
print('Hello Zia, welcome!')
>> Hello Zia, welcome!
■ Indentation is everything in Python. Every line inside the if block must be indented equally. Wrong indentation = IndentationError.
02 if...else Statement
Adds an else block that runs when the if condition is False. One of the two blocks always runs — never both, never neither.
Syntax:
if condition:
# runs if condition is True
else:
# runs if condition is False
Example 1 — pass or fail:
marks = 45
if marks >= 50:
print('Result: PASS')
else:
print('Result: FAIL')
>> Result: FAIL
Example 2 — even or odd:
number = 7
if number % 2 == 0:
print(f'{number} is Even')
else:
print(f'{number} is Odd')
>> 7 is Odd
Example 3 — with user input:
password = input('Enter password: ')
if password == 'python123':
print('Access granted.')
else:
print('Wrong password.')
>> Enter password: hello
>> Wrong password.
03 if...elif...else Statement
elif means 'else if'. Use it when you have more than two possible outcomes. Python checks conditions from top to bottom and runs the first one that
is True, then skips the rest.
Syntax:
if condition1:
# runs if condition1 is True
elif condition2:
# runs if condition2 is True
elif condition3:
# runs if condition3 is True
else:
# runs if ALL above conditions are False
■ You can have as many elif blocks as you need. The else at the end is optional but catches everything that did not match.
Example 1 — grade calculator:
marks = 85
if marks >= 90:
print('Grade: A+')
elif marks >= 80:
print('Grade: A')
elif marks >= 70:
print('Grade: B')
elif marks >= 60:
print('Grade: C')
elif marks >= 50:
print('Grade: D')
else:
print('Grade: F — Fail')
>> Grade: A
Example 2 — time of day greeting:
hour = int(input('Enter hour (0-23): '))
if hour < 12:
print('Good Morning!')
elif hour < 17:
print('Good Afternoon!')
elif hour < 21:
print('Good Evening!')
else:
print('Good Night!')
>> Enter hour (0-23): 14
>> Good Afternoon!
04 Nested if Statements
An if statement placed inside another if statement. The inner if only runs when the outer if is already True.
Syntax:
if condition1:
if condition2:
# runs only if BOTH are True
else:
# runs if condition1 True but condition2 False
else:
# runs if condition1 is False
Example — login with role check:
username = input('Username: ')
password = input('Password: ')
if username == 'admin':
if password == 'secret':
print('Welcome, Admin!')
else:
print('Wrong password.')
else:
print('Username not found.')
>> Username: admin
>> Password: secret
>> Welcome, Admin!
✓ Do not nest too deeply. If you find yourself 3 or 4 levels deep, consider using 'and'/'or' to combine conditions instead.
05 Combining Conditions with and / or / not
Instead of nesting, combine multiple conditions in a single line using logical operators.
and — both must be True:
age = 20
has_id = True
if age >= 18 and has_id == True:
print('Entry allowed.')
else:
print('Entry denied.')
>> Entry allowed.
or — at least one must be True:
day = 'Saturday'
if day == 'Saturday' or day == 'Sunday':
print('It is the weekend!')
else:
print('It is a weekday.')
>> It is the weekend!
not — reverses the condition:
is_raining = False
if not is_raining:
print('Good day to go outside.')
>> Good day to go outside.
Combining all three:
marks = 75
absent = False
if marks >= 50 and not absent:
print('Student has passed.')
>> Student has passed.
06 Ternary / Inline if (One-Line Shortcut)
A compact way to write a simple if...else on a single line. Best used for short, simple decisions — not for complex logic.
Syntax:
value = result_if_true if condition else result_if_false
Example 1:
age = 20
status = 'Adult' if age >= 18 else 'Minor'
print(status)
>> Adult
Example 2 — directly inside print():
marks = 55
print('PASS' if marks >= 50 else 'FAIL')
>> PASS
Example 3 — assigning max of two numbers:
a, b = 10, 25
bigger = a if a > b else b
print(f'Bigger number: {bigger}')
>> Bigger number: 25
■ Ternary is only for simple true/false choices. For multiple conditions (elif), always use the full if/elif/else structure.
07 match...case Statement (Python 3.10+)
Python 3.10 introduced match...case — similar to switch/case in other languages. It matches a variable against several exact values. Cleaner than
many elif checks when comparing one variable to many fixed values.
Syntax:
match variable:
case value1:
# code
case value2:
# code
case _:
# default — runs if nothing matched
Example — day of week:
day = input('Enter day: ')
match [Link]():
case 'monday':
print('Start of the week.')
case 'friday':
print('Almost weekend!')
case 'saturday' | 'sunday':
print('It is the weekend!')
case _:
print('A regular weekday.')
>> Enter day: friday
>> Almost weekend!
■ match...case only works in Python 3.10 and above. Check your version with: python --version
08 Common Mistakes and How to Fix Them
Mistake 1 — using = instead of ==:
if marks = 50: # SyntaxError
if marks == 50: # CORRECT
Mistake 2 — missing colon after condition:
if marks > 50 # SyntaxError: missing colon
if marks > 50: # CORRECT
Mistake 3 — wrong indentation:
if marks > 50:
print('Pass') # IndentationError
if marks > 50:
print('Pass') # CORRECT — 4 spaces indent
Mistake 4 — elif/else without an if:
elif marks > 40: # SyntaxError — no if before this
Mistake 5 — comparing string to int:
age = input('Age: ')
if age > 18: # TypeError — age is a string!
age = int(input('Age: '))
if age > 18: # CORRECT — convert first
■ Summary — All Conditional Statements
Statement Use When Has else?
if One condition to check Optional
if...else Two outcomes: True or False Yes
if...elif...else Three or more outcomes Optional
Nested if Check another condition inside a block Optional
and / or / not Combine multiple conditions in one line —
Ternary (inline if) Simple one-line True/False choice Yes (required)
match...case One variable vs many fixed values case _ (default)
✓ Golden rule: Python reads conditions top to bottom and stops at the FIRST True one. Order your conditions from most specific to most general.
End of Guide — Happy Coding! ■