Quick wins for a faster PC:
Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Clear out junk files and repair common Windows errorsFree Scan →Scan for outdated or missing drivers - takes under a minuteDriver Scan →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.
PC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minuteAdd 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.
#1 Best Overall
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.
Rank #2
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.
The Tool Desk
Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →Use the multi-line statement when a branch contains several actions or when a one-line expression would make the condition harder to understand.
Quick Recap
Best Value
Fix common syntax and logic errors
- Missing colon: End every
if,elif, andelseheader 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
ifchain has at most oneelse, and it comes last. Useeliffor 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/elsestatement.
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.

