if Conditional
Last updated: 2026-09-22
if Conditional
if selects a branch by a boolean condition; pair with elif and else.
| Category | Keywords & Statements |
|---|---|
| Kind | Keyword / Statement |
| Python Version | all |
📝 Syntax
if condition:
block
[elif condition:
block]*
[else:
block]
▶ Example
Edit the code, press Run, and see the result in the output panel:
Output:
positive
💡 Related Cases
More case studies coming soon — check back later.
❓ FAQ
QI wrote `if` and `elif`, and both conditions are true, so why does only one branch run?
A`if`/`elif` is evaluated top to bottom; once it hits the first true branch it stops checking the rest. If you want several conditions to be checked independently, write several separate `if` statements instead of `elif`.
QDoes the condition after `if` need parentheses?
ANo. In Python the condition goes right after `if` and must end with a colon, e.g. `if x > 0:`. Remember that equality is tested with `==`; a single `=` is assignment and raises `SyntaxError`.
QIn `if x:`, when is `x` truthy?
APython uses «truthiness» of `x` as the condition: `0`, `0.0`, empty strings, empty lists, empty dicts, `None` and so on are falsy; everything else is truthy. So `if x:` is equivalent to «x is non-empty / non-zero / not None».
QWhy does `IndentationError` occur when there is no statement under `if`?
ABecause Python uses indentation to delimit code blocks, `if` must be followed by at least one indented statement. If you are not ready to write logic yet, put a `pass` placeholder.