The simplest type of a conditional in Python is the if statement. Let’s explore it in this post.
An if statement consists of a statement header containing a condition and a code block as the statement body.
The code block is executed only if the condition turns out to be true. The condition of an if statement must be an expression that can be interpreted as a truth value (True or False).
if condition:
Statement
…
Statement
As an example, you can consider an if statement that outputs corresponding text only if the variable x has the value 1:
if x == 1:
print("x has value 1")
Of course, you can also use other comparative operators or a more complex logical expression and write more than one statement in the body:
if x < 1 or x > 5:
print("x is less than 1 ...")
print("... or greater than 5")
In many cases, a single if statement is not sufficient and you need a whole chain of mutually exclusive conditionals. In the following example, we want to output two different strings depending on whether x == 1 or x == 2. For this purpose, we can use two consecutive if statements:
if x == 1:
print("x has value 1")
if x == 2:
print("x has value 2")
This is an inefficient way to achieve the goal from the interpreter's point of view, however, because both conditions are evaluated and checked in any case. But the second conditional would no longer have to be considered if the condition of the first one had already yielded True. The variable x cannot have both values 1 and 2 under any circumstances.
To make such cases more efficient from the perspective of the interpreter and clearer from the programmer's point of view, an if statement can be extended by one or more elif branches. The condition of such a branch is evaluated only if all preceding if or elif conditions have been evaluated as False.
You can write the preceding example using elif as follows:
if x == 1:
print("x has value 1")
elif x == 2:
print("x has value 2")
An if statement can be extended by any number of elif branches:
if condition:
Statement
…
Statement
elif condition:
Statement
…
Statement
elif condition:
Statement
…
Statement
In the source code, this could look as follows:
if x == 1:
print("x has value 1")
elif x == 2:
print("x has value 2")
elif x == 3:
print("x has value 3")
As a final extension of the if statement, it’s possible to intercept all previously unhandled cases at once. For example, imagine that we want to output not only a corresponding string if x == 1 or x == 2 applies, but also an error message in all other cases, such as if x == 35 applies. For this purpose, an if statement can be extended by an else branch. If this branch is to be used, it must be written at the end of the if statement:
if condition:
Statement
…
Statement
else:
Statement
…
Statement
In the source code, this can look as follows:
if x == 1:
print("x has value 1")
elif x == 2:
print("x has value 2")
else:
print("Error: The value of x is neither 1 nor 2")
The code block subordinate to the else branch is run only if all previous conditions haven’t been met. An if statement can have only one else branch. In the example, else was used in combination with elif, which is possible but not mandatory.
The following example provides an overview of the structure of an if statement including the possible branch types:
if condition:
Statement
Statement
elif condition:
Statement
Statement
else:
Statement
Statement
If you already know a programming language such as C or Java, you might be interested to know that since Python 3.10 there is a counterpart to the switch/case control structure of these languages—namely, the match/case control structure. In Python versions prior to 3.10, you can mimic the behavior of this control structure by using a cascade of if/elif/else branches.
Editor’s note: This post has been adapted from a section of the book Python 3: The Comprehensive Guide by Johannes Ernesti and Peter Kaiser. Johannes is a research scientist at DeepL. He is a graduate mathematician and received his doctorate in applied mathematics from the Karlsruhe Institute of Technology (KIT). Peter is a research scientist at DeepL. He has a degree in computer science and received a doctorate in humanoid robotics from the Karlsruhe Institute of Technology (KIT).
This post was originally published 7/2025.