Python If

Jakob Jenkov
Last update: 2024-04-13

The Python if instruction is used to execute a given block of instructions only if a given condition is true. In other words, the Python if statement is used to create branches in your code, where the execution will either enter into the if statement and execute its body, it skip over the if statements body and continue execution after.

Here is an example of a Python if statement:

val1 = 23
val2 = 10

if val2 < val1 :
    print("val2 is smaller than val1")

Notice the 4th line in the example above. That line contains the if statement. If the value of the variable val2 is less than the value of the variable val1, then the body of this if statement is executed.

The body of the if statement follows immediately after the if statement itself. In this example the body of the if statement only consists of a single line - a single print() statement.

Notice how the statement inside the if statement body is indented using exactly 4 spaces. All statements inside the body of a Python if statement must be indented exactly 4 spaces. The body of a Python if statement ends at the first line of code - the first statement - that is not indented compared to the if statement. In other words, the first line that has the same indentation level as the if statement itself.

Here is an example of a Python if statement that shows the end of an if statement body:

val1 = 23
val2 = 10

if val2 < val1 :
    print("val2 is smaller than val1")

    print("this is still inside the if body")

print("This is after the if body")

else

The Python else instruction is used in conjuntion with a Python if statement - to execute a set of instructions if a given condition is not true. Here is an example of a Python if statement that also has an else statement (also sometimes referred to as an else-clause):

val1 = 23
val2 = 10

if val2 < val1 :
    print("val2 is smaller than val1")
else:
    print("val2 is not smaller than val1")

print("This is after the if body")

In the above Python if-else example, the if statement's body is executed if val2 is smaller than val1. Else, the body of the else statement is executed.

The rules for indentation of the instructions inside the else body is the same as that of the if statement body.

elif

The Python elif statement is short for "else if". This is an else statement with an if statement attached to it. This means, that the body inside the elif statement is only executed if the first condition of the if statement is false, and the condition of the elif statement is true. Here is an example of a Python elif statement:

val1 = 23
val2 = 10

if val2 < val1 :
    print("val2 is smaller than val1")
elif val2 > val1:
    print("val2 is larger than val2")
else:
    print("val2 is equal to val1")

print("This is after the if body")

Notice how this example also contains an else statement at the end of the if statement. This is not necessary, but it is possible.

The elif statement body will only be executed if val2 is not less than val1 and val2 is larger than val1 .

The else statement body is only executed if val2 is neither smaller than or larger than val1.

if Shorthand Notation

There exists a shorthand version of the Python if instruction notation. Here is how that shorthand if notation looks:

val1 = 23
val2 = 10

if val2 > val1: print("val2 larger than val1")

Notice how the print() statement is put on the same line as the if statement. This is possible if you only have a single line of code to execute within the if statement body.

if else Shorthand Notation

There is also a shorthand notation for the Python if-else construct. Here is an example of this if-else shorthand notation:

val1 = 23
val2 = 10

print("val1 largest") if val1 > val2 else print("val2 largest")

Notice how the first instruction is written before the if-construct, and the else part is written right after the if construct. All the code is written on a single line.

if or

Python has an or instruction that can be used to construct boolean expressions. These or instructions can be used with an if instruction. Here is an example of combining an if instruction and an or instruction in Python:

val1 = 23
val2 = 10

if val1 > 25 or val2 > 5:
    print("True")

Now the print() instruction will be executed if either val1 is larger than 25 (which it is not), or val2 is larger than 5 (which it is). The result is that the print statement is executed since one or the two conditions are true.

if and

Python also has an and instruction that can be used to construct boolean expressions. These and instructions can be used with an if instruction. Here is an example of combining an if instruction and an and instruction in Python:

val1 = 23
val2 = 10

if val1 > 25 and val2 > 5:
    print("True")

Now the print() instruction will be executed if val1 is larger than 25 (which it is not), and val2 is larger than 5 (which it is). The result is, that the print statement is not executed as both conditions are not true.

if Instructions in Other Languages

The if instruction is a very common instruction found in many other programming languages. Here are some links to tutorials about if instructions in languages covered here at jenkov.com :

Jakob Jenkov

Featured Videos

Java ForkJoinPool

P2P Networks Introduction




















Advertisements

High-Performance
Java Persistence
Close TOC
All Tutorial Trails
All Trails
Table of contents (TOC) for this tutorial trail
Trail TOC
Table of contents (TOC) for this tutorial
Page TOC
Previous tutorial in this tutorial trail
Previous
Next tutorial in this tutorial trail
Next