-
Integers, Floats and Strings 3
-
Lecture1.1
-
Lecture1.2
-
Lecture1.3
-
-
If Statements 3
-
Lecture2.1
-
Lecture2.2
-
Lecture2.3
-
-
Lists, Sets and Tuples 5
-
Lecture3.1
-
Lecture3.2
-
Lecture3.3
-
Lecture3.4
-
Lecture3.5
-
-
Loops 3
-
Lecture4.1
-
Lecture4.2
-
Lecture4.3
-
-
Functions 3
-
Lecture5.1
-
Lecture5.2
-
Lecture5.3
-
-
Dictionaries 3
-
Lecture6.1
-
Lecture6.2
-
Lecture6.3
-
-
Assertions 2
-
Lecture7.1
-
Lecture7.2
-
-
Classes 3
-
Lecture8.1
-
Lecture8.2
-
Lecture8.3
-
-
Matplotlib 2
-
Lecture9.1
-
Lecture9.2
-
Advanced If Statements
Else Statement¶
The else statement follows the if statement and gives code to execute when the if statement is not true. The format of it is:
if boolean:
execute code
else:
execute other code
#Else gets executed when our if statement is not true
a = 5
if a==6:
print("If Statement")
else:
print("Else Statement")
Else Statement
#Else does not get executed here
a = 6
if a==6:
print("If Statement")
else:
print("Else Statement")
If Statement
Elif Statement¶
By using elif, we get to check if another condition is true after we have seen that the if statement is in fact not true. We can use it with or without an else statement after it. The code below first checks if a equals 5 and prints Good if it is, otherwise it will check if it is equal to 6 and print Meh it is, and finally in all other cases it will print bad.
#elif lets us check other conditions if the statement above it is false
a = 6
if a==5:
print("Good")
elif a==6:
print("Meh")
else:
print("Bad")
Meh
a = 10
if a==5:
print("Good")
elif a==6:
print("Meh")
elif a==10:
print("Bad")
Bad
You can use as many elif statements as you please, each gets checked after the one before it comes back false and only if it comes back false. Below, we will check if a is equal to 5, then if it is equal to 6, then if it is equal to 10. If none of these are true, nothing happens.
#Nothing gets printed because none of these statements are true
a = 11
if a==5:
print("Good")
elif a==6:
print("Meh")
elif a==10:
print("Bad")
A final note on elif. When one of the conditions is true, it no longer checks anything afterwards. In the code below, it will not print the condition checking if a >= 0 because the condition a == 6 already was true.
a = 6
if a==5:
print("Good")
elif a==6:
print("Meh")
elif a >= 0:
print("This is a positive number")
Meh
And/Or¶
The and statement takes two boolean values and returns true only if both are true. The or statement returns true if either of the statements are true. Below we can see that the only case where we get true for an and statement is when both are true.
print(True and True)
print(False and True)
print(True and False)
print(False and False)
True
False
False
False
Of course we can use conditions as well...
print(5>6 and 5<6)
print(True and 5==5)
False
True
Notice how or is only false if both of the boolean values are false.
print(True or True)
print(False or True)
print(True or False)
print(False or False)
True
True
True
False
Once again we can have conditions
print(5>6 or 5<6)
print(False or 5==5)
True
True