if
-elif
-else
statement¶
The code block after the first true condition of an if
or elif
statement
is executed. If none of the conditions are true, the code block after the
else
is executed:
1>>> x = 1
2>>> if x < 1:
3... x = 2
4... y = 3
5... elif x > 1:
6... x = 4
7... y = 5
8... else:
9... x = 6
10... y = 7
11...
12>>> print(x, y)
136 7
- Lines 5 and 8
The
elif
andelse
clauses are optional, and there can be any number ofelif
clauses.- Lines 3, 4, 6, 7, 9 and 10
Python uses indentations to delimit blocks. No explicit delimiters such as brackets or curly braces are required. Each block consists of one or more statements separated by line breaks. All these statements must be on the same indentation level.