Python Basics
Python Conditionals
1. What are Conditionals?
- Allow you to conditionally run code based on whether a condition is
TrueorFalse - Three keywords:
if,elif,else - Only one block in an if/elif/else chain will ever run
2. The if Statement
Syntax
if condition:
# code to run if condition is True
# must be indented (4 spaces or 1 tab)
⚠️ Indentation is mandatory — Python uses indentation to know what's inside the
ifblock. Wrong indentation = error.
Example
name = input("Name: ")
if name == "Tim":
print("Hello, Tim!")
print("Hello, Tim!") # multiple lines are fine
- If condition is
True→ block runs - If condition is
False→ block is skipped entirely
3. The else Statement
- Runs when the
ifcondition isFalse - Must come directly after an
if(orelif) block - Same indentation level as
if - Optional — you don't always need one
number = float(input("Enter a number: "))
if number < 5:
print("Good job!")
else:
print("Not a good job!")
print("Finished") # runs regardless — NOT part of the if/else
Code at the same indentation level as
ifalways runs — it's outside the conditional block
4. The elif Statement
- Stands for "else if"
- Adds additional conditions to check if the previous ones were
False - Must come after
if, and beforeelse - You can have as many
elifs as you want - As soon as one condition is
True, that block runs and the rest are skipped
number = float(input("Enter a number: "))
if number < 0:
print("This is a negative number")
elif number < 10:
print("This is less than 10")
elif number < 20:
print("This is less than 20")
else:
print("Not a good job!")
Execution Flow
Check if → True? Run block, EXIT entire chain
→ False? ↓
Check elif → True? Run block, EXIT entire chain
→ False? ↓
Check elif → True? Run block, EXIT entire chain
→ False? ↓
else → Run this block
5. Compound Conditions in if Statements
- Use
and,or,notto combine multiple checks
# Check if number is positive AND even
if number > 0 and number % 2 == 0:
print("This is a positive, even number!")
Checking if a number is even:
number % 2 == 0Checking if a number is odd:number % 2 != 0
6. Nested if Statements
- You can place
ifstatements inside otherifstatements - Inner
ifonly runs if the outerifcondition wasTrue - Each level needs its own indentation
number = float(input("Enter a number: "))
if number > 0 and number % 2 == 0:
print("Positive even number!")
number2 = float(input("Enter another number: "))
if number2 < 0:
print("This is negative")
else:
print("This is positive")
7. Inline (One-Line) if Statement
- Compact syntax for assigning a value based on a condition
- Useful when the logic is simple
Syntax
result = value_if_true if condition else value_if_false
Examples
x = 5
result = "okay" if x > 5 else "not okay"
print(result) # "not okay"
x = 6
result = "okay" if x > 5 else "not okay"
print(result) # "okay"
Also works with statements (not just assignments):
print("Hello") if x > 5 else print("Not okay")
8. Full if / elif / else Template
if condition1:
# runs if condition1 is True
elif condition2:
# runs if condition1 is False AND condition2 is True
elif condition3:
# runs if condition1 & 2 are False AND condition3 is True
else:
# runs if ALL conditions above are False
# code here runs regardless
9. Key Takeaways & Recap
ifruns a block only when its condition isTrue- Indentation (4 spaces / 1 tab) is required — Python enforces it
elsehandles the case when theifcondition isFalse— it is optionalelifchecks additional conditions — must go betweenifandelse- Only one block in an
if/elif/elsechain ever runs - Code outside the chain (same level as
if) always runs - Compound conditions (
and,or,not) work insideifstatements - Inline
if:value_if_true if condition else value_if_false