← Back to Blog

if / elif / else — The Full Picture

The idea!

You've covered if, elif, and else. You've built decision systems, handled multiple conditions, and combined them with and.

This is your reference. Everything in one place.

Come back here whenever you need a reminder.

if — the basic decision

Runs a block only when the condition is true.

score = 85

if score >= 60:
    print("Passed.")    # runs — condition is True

if / else — both sides covered

One block runs. Always. No silence.

score = 45

if score >= 60:
    print("Passed.")
else:
    print("Failed.")    # runs — condition is False

if / elif / else — multiple conditions

Python checks top to bottom. First match wins. Most specific condition goes first.

score = 72

if score >= 90:
    print("Excellent.")
elif score >= 70:
    print("Good.")      # runs
elif score >= 60:
    print("Passed.")
else:
    print("Failed.")

Comparison operators

x = 10

if x == 10:    # equal to
if x != 10:    # not equal to
if x > 10:     # greater than
if x < 10:     # less than
if x >= 10:    # greater than or equal
if x <= 10:    # less than or equal

Logical operators

Combine multiple conditions in a single line.

age = 30
window = "y"

if window == "y" and age >= 12:    # both must be True
    price += 3

if age < 12 or age >= 65:          # at least one must be True
    print("Discounted ticket.")

if not age >= 18:                   # inverts the condition
    print("Access denied.")

Strings in conditions

Comparisons are case-sensitive. Normalize before comparing.

answer = input("Continue? (y/n): ")

if answer.lower() == "y":
    print("Continuing...")

+= shorthand

price = 12
price += 3     # same as price = price + 3
print(price)   # 15

Common Mistakes

  • = assigns, == compares — never mix them in a condition
  • Every if, elif, else line ends with :
  • Indentation is not style — it's syntax. Always 4 spaces
  • Most specific conditions always go first
  • String comparisons are case-sensitive — .lower() is your friend
  • else catches everything — make sure you know what that includes

What you should understand now

  • if runs a block only when the condition is True
  • else is the fallback — one block always runs
  • elif chains multiple conditions — first match wins
  • and, or, not combine conditions in a single line
  • Order matters — most specific condition always goes first
  • Strings are case-sensitive — normalize before comparing
[ login to bookmark ] // copied! 33 views · 2 min
// resources
Cheatsheet if / elif / else — The Full Picture
← prev Common IF Mistakes next → This One's Yours — IF
// 0 comments
// No comments yet. Be the first.
// leave a comment

// Your comment will appear after approval.