Logical conditions and If statements are an extremely important part of every programming language because they allow the program to execute specific part of codes based on some events. In this way the program can react differently to different events making it able to solve more complex tasks.
The If statement is created by using the keyword if
and then conditions. The conditions available in Python between two variables $a1$ and $a2$ are:
a1 == a2
a1 > a2
a1 < a2
a1 >= a2
a1 <= a2
a1 != a2
The code below show some examples of if statements where the print function is executed because $a1$ and $a2$ are different. It is important to use the indentation (typically by using Tab) after an If statement because this is the way Python divides blocks of code.
a1=10 a2=5 if a1 != a2: print("a1 is different from a2")
The If statement can be enriched by using the elif
keyword which checks another condition. In this way it is possible to associate different blocks of code to different conditions within the same If statement.
a1=20 a2=20 if a1 != a2: print("a1 is different from a2") elif a1 == a2: print("a1 is equal to a2") # This is executed because they have the same values
The last keyword that can be added into the If statement is else
. This is an important keyword because the code executed within "else" is the one that did not satisfy any of the if
and elif
conditions. The only keyword required in an If statement is if
, the other two keywords are optional.
#Example 1 a1=2 a2=4 if a1/a2 > 1: print("ratio > 1") elif a1/a2 == 1: print("ratio = 1") else: print("ratio < 1") # This is executed because the ratio is 0.5 #Example 2 if a1*a2 > 10: print("product greater than 10") else: print("product less than 10") # This is executed because the product is 8
The If statements can be also created within other If statements in order to be able to create more complex behaviors.
a1=3 a2=5 a3=8 if a1+a2 > 5: print("sum is greater than 5") # This is executed because the sum is 8 if a1+a2 == a3: print("sum is equal to a3") # This is also executed because the sum is equal to a3 else: print("sum is different from a3") else: print("sum is less than or equal to 5")
In case more than one logical condition needs to be taken into account then Python uses the logical operators and
and or
.
If the and
operator is used within two conditions, they need to be both True to execute the code within the If statement. It is also possible to use more than two conditions and they need to be all True to execute the code.
a1=12 a2=10 #Example 1 if a1 > a2 and a1/a2 < 2: print("ratio > 1 and < 2") #Example 2 if a1 > a2 and a1/a2 < 2 and a1 > 15: print("ratio > 1 and < 2. also, a1 > 15") else: print("one of the conditions is not true") # This is executed because a1 < 15
If the or
operator is used within two conditions, at least one needs to be True to execute the code within the If statement. It is also possible to use more than two conditions and at least one need to be True to execute the code.
a1=12 a2=10 #Example 1 if a1 > a2 or a1>15: print("ratio > 1 or a1>15") # This is executed because the ratio > 1 although a1 < 15 #Example 2 if a1/a2 > 2 or a1 == 5 or a2 == 20: print("ratio > 2 or a1=5 or a2=20") else: print("all the conditions are not true") # This is executed because all the conditions are false
To create complex behaviors we can use both the and
and or
operators like in the examples below. Note that we can use the parentheses like in mathematics in order to force a specific order of executions of the comparisons.
a1=5 a2=10 a3=15 if (a1*2==a2 and a2*2==a3) or (a1+5==a2 and a2+5==a3): print("one of the two conditions within parentheses is true") # This is executed because the conditions in the second parentheses are true else: print("both the conditions are false")
This concludes the introduction of logical conditions and if statements, try to code different conditions and statements with the online Python console below.