All Courses
Python Basics

Python Conditionals

1. What are Conditionals?

  • Allow you to conditionally run code based on whether a condition is True or False
  • 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 if block. 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 if condition is False
  • Must come directly after an if (or elif) 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 if always 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 before else
  • 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, not to 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 == 0 Checking if a number is odd: number % 2 != 0


6. Nested if Statements

  • You can place if statements inside other if statements
  • Inner if only runs if the outer if condition was True
  • 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

  1. if runs a block only when its condition is True
  2. Indentation (4 spaces / 1 tab) is required — Python enforces it
  3. else handles the case when the if condition is False — it is optional
  4. elif checks additional conditions — must go between if and else
  5. Only one block in an if/elif/else chain ever runs
  6. Code outside the chain (same level as if) always runs
  7. Compound conditions (and, or, not) work inside if statements
  8. Inline if: value_if_true if condition else value_if_false