Conditionals Jump to this section

Conditionals let your program make decisions, running different code depending on whether something is true or false.

Comparison operators Jump to this section

Use these to compare values (they return True or False):

  • == equal to
  • != not equal to
  • > greater than
  • < less than
  • >= greater than or equal to
  • <= less than or equal to

Examples:

print(3 == 3)   # True
print(4 != 5)   # True
print(7 > 10)   # False

Common mistake: Always use == to compare values. = is for assigning a value to a variable.


Basic if / else Jump to this section

Run code only when a condition is true.

age = 18
if age >= 18:
    print("You can vote") # this line ONLY runs if the condition is true
else:
    print("You are too young to vote")

Important: Python uses indentation (spaces) to group the code inside the if or else blocks.


elif (else if) Jump to this section

Check multiple conditions in order.

score = 75
if score >= 90:
    print("A")
elif score >= 80:
    print("B")
elif score >= 70:
    print("C")
else:
    print("Failing")

Python checks conditions from top to bottom.
As soon as one condition is true, that code runs and the rest is skipped.


Logical Operators: and, or, not Jump to this section

Use these to create special conditions for if statements:

  • and: all must be true
  • or: at least one must be true
  • not: reverses True/False
age = 20
has_id = True
if age >= 18 and has_id:
    print("Allowed")

if age < 13 or age > 65:
    print("Special pricing")

if not has_id:
    print("Please show ID")

Truthiness Jump to this section

Values can be treated as True or False in conditions:

  • Numbers: 0 is False, any nonzero is True
  • Empty containers ("", [], {}) are False; non-empty are True
  • None is False
if "":
    print("won't print")
if [1,2]:
    print("list is truthy")

Truthy just means that it evaluates to True

This is useful when checking if something exists or has content:

name = ""
if name:
    print("Name provided")
else:
    print("Name is empty")

Nested Conditionals Jump to this section

You can put if blocks inside other if blocks,

x = 10
if x > 0:
    if x % 2 == 0:
        print("positive even")
    else:
        print("positive odd")

Remember, modulus (%) gives the remainder

Make sure to keep code readable. If nesting gets deep, consider:

  • using elif instead
  • breaking logic into smaller steps

Challenge Jump to this section

Write a program that asks the user for a number and prints whether it's positive, negative, or zero.

Try to use if, elif, and else.

Hint 1: Converting input

Remember to convert the user's input to a number using int() or float() before comparing.

Hint 2: Compare in the right order

Check > 0 first for positive, < 0 for negative, and use else for zero (or check == 0).


Show example solution
number = float(input("Enter a number: "))
if number > 0:
    print("Positive")
elif number < 0:
    print("Negative")
else:
    print("Zero")

Key Takeaways Jump to this section

  • Use comparison operators (==, !=, >, <, >=, <=) to build conditions.
  • if, elif, and else let your program choose one path of code to run.
  • Combine conditions with and, or, and not to express complex rules.
  • Python treats some values as truthy/falsey — empty containers and 0 are falsey.
  • Keep conditions simple and readable; test edge cases like zero or empty input.