Skip to content

Condition

In one line

A condition is an expression that evaluates to True or False, used to control which branch of code executes.

In simple words

A condition is an expression that evaluates to a boolean (True or False). Conditions control program flow with if, elif (else if), and else statements.

Python evaluates conditions from top to bottom. The first true condition's block runs, and the rest are skipped. elif and else are optional. Every if block must be indented.

Comparison operators produce conditions: == (equal), != (not equal), >, <, >=, <=. Logical operators combine conditions: and (both true), or (either true), not (invert). Membership (in) and identity (is) also produce booleans.

Example

python
score = 85

if score >= 90:
    grade = "A"
elif score >= 80:
    grade = "B"
elif score >= 70:
    grade = "C"
else:
    grade = "F"

print(grade)  # B

# Combined conditions
age = 25
has_id = True
if age >= 18 and has_id:
    print("Entry allowed")

Conditions use if/elif/else with comparison and logical operators to control flow.

Common confusions

  • Confused with: loop

    The difference: A condition (if) runs a block once if the test is true. A loop (while) runs a block repeatedly as long as the test is true. Conditions make decisions; loops make repetitions.

Related terms

Related Resources

Related Lessons

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary