Conditional

Code that runs only when a condition is true (if / elif / else).

A conditional chooses which code to run based on a Boolean test. if runs its block when the condition is true; optional elif branches test further conditions; else catches everything left over. Only the first matching branch runs.

Example
score = 72
if score >= 90:
    grade = "A"
elif score >= 60:
    grade = "pass"
else:
    grade = "fail"
print(grade)
Output
pass

Where this shows up in real Python

Conditionals are behind every decision a program makes: validating input, choosing a code path, handling edge cases.

Conditional tools

  • if / elif / else — branch on one or more conditions
  • and / or / not — combine conditions
  • x if cond else y — a one-line conditional expression
  • ==, in, <, > — the tests that produce True/False

Official documentation: Python Tutorial: if Statements

Related lessons

Related terms