Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Use if to test a condition, elif to test additional alternatives, and else to handle everything left over. Python checks the branches in order and runs the suite belonging to the first true condition; in an if/elif/else chain, only one suite is selected.

Write a basic if-else statement

An if statement begins with the keyword, a condition, and a colon. Put the statements to run on the next line and indent them consistently. An optional else clause provides a fallback when the condition is false.

age = 17

if age >= 18:
    print("Adult")
else:
    print("Not an adult")

Here, Python tests whether age >= 18 is true. Because age is 17, the indented else suite runs. The Python tutorial explains that an if statement can have zero or more elif parts and an optional else part: Python Tutorial: More Control Flow Tools.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Add alternatives with elif

Use elif when you need to check another condition if the earlier one was false. Python evaluates the chain from top to bottom, stopping at the first true condition. If none is true and an else is present, its suite runs.

score = 82

default_grade = "Below B"

if score >= 90:
    grade = "A"
elif score >= 80:
    grade = "B"
else:
    grade = default_grade

print(grade)

The result is B: the first condition is false, and the second is true. When conditions overlap, arrange them so a more specific or higher-threshold case is checked before a broader one. Otherwise, the broader condition might match first and prevent a later branch from being reached.

Choose a value with a conditional expression

If each branch only chooses one value, you can use Python’s conditional expression: value_if_true if condition else value_if_false.

age = 17
label = "adult" if age >= 18 else "minor"

Python evaluates the condition first, then evaluates and returns the value on the matching side. Unlike a statement, a conditional expression produces a value, so it can be assigned to a variable or used within another expression. The language reference defines this form as x if C else y: Python Language Reference: Conditional Expressions.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use the multi-line statement when a branch contains several actions or when a one-line expression would make the condition harder to understand.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Fix common syntax and logic errors

  • Missing colon: End every if, elif, and else header with :.
  • Inconsistent indentation: Indent each branch’s body consistently so Python can identify which statements belong to it.
  • More than one else at the same level: An if chain has at most one else, and it comes last. Use elif for additional conditions.
  • Conditions in the wrong order: If a broad condition is true for values that should match a narrower case, put the narrower test first.
  • Assignment instead of comparison: Use == to test equality in a condition; = assigns a value to a variable.
  • Unnecessarily compact logic: Replace a hard-to-read conditional expression with a regular if/else statement.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.