if
KeywordPython 2.0+Beginner
Starts a conditional statement; executes block only if condition is true
Quick Info
- Documentation
- Official Docs
- Python Version
- 2.0+
Learn by Difficulty
Quick Example
python
temperature = 35 if temperature > 30: print("It's hot!") x = 10 if x > 0: print("Positive") elif x == 0: print("Zero") else: print("Negative")
if evaluates a condition. The indented block runs only when the condition is True.
Try in PlaygroundTags
languagesyntaxcorecontrol-flowconditional
Related Items
elif
Keyword
Short for 'else if'; adds another condition to an if chain
else
Keyword
Catch-all branch when preceding if/elif conditions are all false
and
Keyword
Logical AND operator; returns True if both operands are true
or
Keyword
Logical OR operator; returns True if at least one operand is true
not
Keyword
Logical NOT operator; inverts a boolean value
match
Soft Keyword
Begins a structural pattern matching block (3.10+)