Featureintro
If Statements
Also called if statement, if elif else, elif, conditional statement in Python, else if in Python, switch in Python.
An if statement tests its conditions one by one and runs the block of the first true condition. It can have zero or more elif parts and one optional else part, which runs when no condition is true.
- Language
- Python
Synopsis
def sign(x):
if x < 0:
return "negative"
elif x == 0:
return "zero"
else:
return "positive"
print(sign(-5), sign(0), sign(7))
negative zero positive
Python tests x < 0 first, then x == 0. When no condition is true, the else block runs.
Semantics
r1. The if statement "selects exactly one of the suites by evaluating the expressions one by one until one is found to be true". Then "that suite is executed (and no other part of the if statement is executed or evaluated). If all expressions are false, the suite of the else clause, if present, is executed."2 Examples: sign, first-true.
r2. "There can be zero or more elif parts, and the else part is optional. The keyword ‘elif’ is short for ‘else if’, and is useful to avoid excessive indentation."1
r3. A condition does not have to be a bool. "In the context of Boolean operations, and also when expressions are used by control flow statements, the following values are interpreted as false: False, None, numeric zero of all types, and empty strings and containers".3 "All other values are interpreted as true."3 Example: truth.
r4. The condition is an assignment_expression in the grammar, so it can contain :=.2 Example: walrus.
r5. "An if … elif … elif … sequence is a substitute for the switch or case statements found in other languages."1 To compare one value with several constants, the tutorial also suggests the match statement.1
Examples
def check(name, result):
print("test", name)
return result
if check("a", False):
print("a")
elif check("b", True):
print("b")
elif check("c", True):
print("c")
test a
test b
b
Python does not evaluate the third condition, because the second condition is true (rule r1).
for value in [0, 1, "", "text", [], [0], None]:
if value:
print(repr(value), "is true")
else:
print(repr(value), "is false")
0 is false
1 is true
'' is false
'text' is true
[] is false
[0] is true
None is false
data = [3, 1, 4, 1, 5]
if (n := len(data)) > 3:
print(f"{n} items is too many")
5 items is too many
Pitfalls
= is not a comparison, and a condition cannot contain a plain assignment:
This code raises SyntaxError, on purpose.
x = 1
if x = 1:
print("one")
A test of one value against several values needs a full comparison for each value:
This code runs but gives a wrong answer on purpose.
color = "blue"
if color == "red" or "green":
print("warm or green")
warm or green
The condition is (color == "red") or "green". The string "green" is not empty, so the condition is true for each color (rule r3). Write color in ("red", "green") or color == "red" or color == "green".
Comparison
In Go, "If statements specify the conditional execution of two branches according to the value of a boolean expression." The Go condition must be a boolean expression, and "The expression may be preceded by a simple statement, which executes before the expression is evaluated."4 Go uses else if, and it needs braces around each block. Python uses elif and indentation, and its condition can have any type (rule r3).
References
- The Python Tutorial, 4. More Control Flow Tools, 4.1. if Statements
- The Python Language Reference, Python 3.15, 8.1. The if statement
- The Python Language Reference, Python 3.15, 6.11. Boolean operations
- The Go Programming Language Specification, If statements
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag T2VHG5 Edit this page History Report an error Markdown