Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk2 min

Python if-else: How to Choose Which Code Runs

Use Python’s if, elif, and else clauses to run the first matching code block, or choose between two values with a conditional expression.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Python’s if and else let a program choose between blocks of code. Add elif when there are more conditions to check. In each chain, Python runs only the suite for the first condition that is true, or the optional else suite if none is true.

How an if-else statement works

Start with if, write a condition, and end the line with a colon. Indent the code that should run when the condition is true. An optional else clause provides the alternative when the condition is false.

As an Amazon Associate I earn from qualifying purchases.

age = 17

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

Here, age >= 18 is false, so Python runs the indented else body. The colon marks the start of each clause’s body; indentation shows which statements belong to it.

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

How to check more than two cases with elif

Use elif (“else if”) to add another condition between if and else. Python checks the clauses from top to bottom and selects only the first suite whose condition is true. The else clause is optional, and a chain can contain zero or more elif clauses, as described in the Python 3.14 tutorial.

score = 82

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

print(grade)

This prints B: the first test is false, and the second is true. Put more specific or higher-threshold conditions first when they overlap. For example, if the score >= 80 test came before score >= 90, a score of 95 would match the former and the later branch would never be reached.

When to use a conditional expression

If the choice is just between two values, Python also offers a conditional expression, sometimes called a ternary expression:

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

It follows the form x if condition else y: Python evaluates the condition, then evaluates and returns x if it is true or y if it is false. It is an expression, so it can appear where a value is expected. The Python language reference defines this behavior. Use a regular multi-line statement when a branch needs several actions or the one-line form would be hard to read.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Errors and readability checks

  • Missing colon: Put a colon after each if, elif, and else header.
  • Inconsistent indentation: Indent each branch body consistently so Python can identify the suite it belongs to.
  • More than one else: A chain can have only one else at that level. Use elif for additional conditions.
  • Overlapping conditions in the wrong order: Check narrower or higher-threshold conditions before broader ones.
  • Assignment instead of comparison: Use == to test whether two values are equal; = assigns a value.
  • Overcomplicated one-liner: Prefer an ordinary if/elif/else statement when it makes the logic easier to follow.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Free tools Windows power users keep installed

One-click scans. No signup required.

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

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.