A boolean variable takes on values of True or False. Take a look at line 6 in the example below. In this case we are asking if integer 4 is equal to float 4.0000 The answer is False. This has to do with the different way in which integers and floats are stored in the computer. Never compare integers and floats in logical expressions.
# Ex1.4.1
x=4
y=3
z=4.0000
print(x>y)
print(x==y)
print(z==y)
print(y<=z)
True False False True
In the preceeding example we used the following relational operators
Operator | Operation |
---|---|
< | Less than |
<= | Less than or equale |
== | equal |
> | Greater than |
>= | Greater than or equal |
!= | Not equal |
Python has three Boolean (logical) operators: and, or, not
# Ex1.4.2
x=True
y=False
z=True
print(x and z)
print(x or y)
print(x and not y)
True True True
The relational operators >, <, ==, != can be combined in boolean expressions.
# Ex1.4.3
x=7
y=-2
z=8
print(x > 8 and z >y)
print(x > 8 or z >y)
print(x*z > abs(y*z) and y != 0)
False True True
Reference: W3schools Python If statement
The if statement executes a block of code if the condition is True. Take a look at the following code. First, note that the variables A and a are two different variables. Clearly, A>a. In Python, the if condition is followed by a colon : and the code to be executed if True is indented. Some computer languages frame the block of code with brackets {} while others may use begin and end to delimit the code if True. In Python, the indentation of white space delimits the code of True. This can be a single statement like Ex1.4.4 or it can be multiple statements set off by white space. Note, most Python development environments will automatically indent once you type the colon :
# Ex1.4.4
A=8
a=7
if A>a :
print(A,">",a)
8 > 7
In some cases we want one action if True and another if False. In this case use the if/else structure.
# Ex1.4.5
A=6
a=7
if A>a :
print(A,">",a)
else:
print(A,"is not greater than",a)
6 is not greater than 7
We may want to differentiate three different situations. In the following example A and a are both equal integer values. Theoretically, when comparing integers there are three conditions that involve are they (1) greater than, (2) equal or (3) less than. The elif picks up the case in which the two variables are equal and the final else picks up everything else (that is in this case that A is less than a).
NOTE: IT IS NOT ELSE IF BUT ELIF
# Ex1.4.6
A=6
a=6
if A>a :
print(A,">",a)
elif A==a:
print(A,"=",a)
else:
print(A,"is less than a")
6 = 6
The elif
Use a simple else when you do not need to branch based on a boolean expression.
The if/else construct has one if and one else. Using elif we can embed as many decisions following if. As in example 1.4.4 the else is NOT REQUIRED.
# Ex 1.4.7
fruit = 'orange'
if fruit == 'apple':
print("fruit is an apple")
elif fruit == "pear":
print("fruit is a pear")
elif fruit == "orange":
print("fruit is an orange")
else:
print("fruit isn't an apple, pear or orange")
fruit is an orange
Many computer languages have a switch statement that allows selecting different actions depending on a variable. In Python we can use the match/case construct. In EX 1.4.8 we rewrute the previous example. The the example below match is the variable fruit. Line 4 is equialent to the boolean expression if fruit == 'apple': It is comparing the match variable to a case variable.
# Ex 1.4.8
fruit='pear'
match fruit:
case 'apple':
print("fruit is an apple")
case "pear":
print("fruit is an pear")
case "orange":
print("fruit is an orange")
fruit is an pear