In Python three logical operators are:
and,
or and
not. These operators are used to combine conditional statements. The meaning of these operators is the same as to their meaning in English. The
and logical operator returns True if both statements are true. All possible cases with outputs of logical operator
and are shown in Table 1.
Table 1 - possible outcomes of using and logical operator
Case number | First expression | and | Second Expression | Output |
1 | True | and | True | True |
2 | True | and | False | False |
3 | False | and | True | False |
4 | Flase | and | False | False |
In the
Example 1 we are going to experiment with
and logical operator, only.
Example 1 Print out the output of the following statements.
- x1 = 6; x1 < 5 and x1 > 10; x1 > 5 and x1 < 8
- x2 = 3; x2 < 5 and x2 > 1; x2 > 0 and x2 > 100
- x3 = 10; x3 < 1000 and x3 > 5; x3 > -1 and x3 < -10
Solution: First we are going to define all the variables which are x1, x2, x3 and assign integers 3, 6, 10 to them respectively.
x1 = 6
x2 = 3
x3 = 10
Now we have to write the code for printing out the result of logical operator "and".
print("x1 < 5 and x1 > 10 is ",x1 < 5 and x1 > 10)
print("x1 > 5 and x1 < 8 is ",x1 > 5 and x1 < 8)
print("x2 < 5 and x2 > 1 is ", x2 < 5 and x2 > 1)
print("x2 > 0 and x2 > 100 is ", x2 > 0 and x2 > 100)
print("x3 < 1000 and x3 > 5 is ",x3 < 1000 and x3 > 5)
print("x3 > -1 and x3 < -10 is ",x3 > -1 and x3 < -10)
After runing previous code the following output is generated.
x1 < 5 and x1 > 10 is False
x1 > 5 and x1 < 8 is True
x2 < 5 and x2 > 1 is True
x2 > 0 and x2 > 100 is False
x3 < 1000 and x3 > 5 is True
x3 > -1 and x3 < -10 is False
In the 1. code line we wanted to see if x1 is less than 5 and greated than 10. The x1 is equal to 6 the first term (x1 < 5) is false since 6 is greater than 5, and the second term (x1 > 10) is also false since x1 is lower value than 10. Since both terms are False the output will be False.
x1 < 5 and x1 > 10 is False
In the 2. code line the first expression (x1 > 5) is True since x1 is greater than 5, and second expression (x1 < 8) is also True since 8 is greater than x1. So both expressions are True which means that output of the logical operator
and will also be True.
x1 > 5 and x1 < 8 is True
In the 3. code line the first expression (x2 < 5) is True since x2 is less than 5 and the second expression (x2 > 1) is also True since x2 is greater than 1. So, both expressions are True which means that output of the logical operator
and will also be True.
x2 < 5 and x2 > 1 is True
In the 4. code line the first expression (x2 > 0) is True since x2 is greater than 0, and second expression (x2 > 100) is False since x2 is less than 100. So the first expression is True and the second expression is False which means that the output of logical operator
and will be False since both statements must be True so that the output of
and is True.
x2 > 0 and x2 > 100 is False
In the 5. code line the first expression (x3 < 1000) is True since x3 is less than 1000, and the second expression (x3 > 5) is True since x3 is greater than 5. So, both expressions are True which means that the output of logical operator
and would also be True.
x3 < 1000 and x3 > 5 is True
In the 6. code line the first expression (x3 > -1) is True since x3 is greater than -1, and the second expression (x3 < -10) is False since x3 is greater than -10. So, the first expression is True and the second expression is False. The output of logical operator
and will be False since both expressions must be True so that the output of
and operator would be True.
x3 > -1 and x3 < -10 is False
The logical operator
or will return True if one of the statements is True. All cases are given in the following Table 2.
Table 2 - possible outcomes of or logical operator.
Case number | First expression | or | Second Expression | Output |
1 | True | or | True | True |
2 | True | or | False | True |
3 | False | or | True | True |
4 | Flase | or | False | False |
Example 2 Solve the following problems (x1 = 6; x2 = 3; x3 = 10)
- x1 < 4 or x1 >= 6
- x1 > 5 or x1 < 1
- x2 > 10 or x2 < 5
- x2 > 1 or x2 < 6
- x3 > 22 or x3 > 11
- x3 > 9 or x3 < 11
Solution: The first step is to define variables (x1, x2, x3) and to assign values. Than the previously given cases with
or logical operator will be tested and the output will be printed out.
x1 = 6; x2 = 3; x3 = 10
print("x1 < 4 or x1 >= 6? The result is", x1 < 4 or x1>=6)
print("x1 > 5 or x1 < 1? The result is",x1 > 5 or x1 < 1)
print("x2 > 10 or x2 < 5? The result is",x2 > 10 or x2 < 5)
print("x2 > 1 or x2 < 6? The result is",x2 > 1 or x2 < 6)
print("x3 > 22 or x3 > 11? The result is",x3 > 22 or x3 > 11)
print("x3 > 9 or x3 < 11? The result is",x3 > 9 or x3 < 11)
The generated ouput from the previous code block is given below.
x1 < 4 or x1 >= 6? The result is True
x1 > 5 or x1 < 1? The result is True
x2 > 10 or x2 < 5? The result is True
x2 > 1 or x2 < 6? The result is True
x3 > 22 or x3 > 11? The result is False
x3 > 9 or x3 < 11? The result is True
In the 1. code line the variables x1, x2 and x3 are define with values assigned to them in the following order 6, 3 and 10, respectively. In the 2. code line the expression
x1 < 4 or x1 >= 6 is tested and the result is printed out using built in function print. The first expression (x1 < 4) in the 2. code line is False since x1 is greater than 4 and the second expression (x1 >= 6) is True since x2 is equal to 6. Between previous two expression is logical operator
or so at least one expression must be True and the output would be True. In this case the first expression is False, second expression is True so the main output is True.
x1 < 4 or x1 >= 6? The result is True
In the 3. code line the expression
x1 > 5 or x1 < 1 is tested and the output is printed out. The first expression (x1 > 5) in the 3. code line is True since the x1 is greater than 5, and the second expression (x1 < 1) is False since the x1 is greater than 1. Between those two expressions is logical operator
or so the one of the expressions must be True which will result in output value True. In this case the frist expression is True, second is False so the output is True.
x1 > 5 or x1 < 1? The result is True<
In the 4. code line the expression
x2 > 10 or x2 < 5 is tested and the result is printed out. The first expression (x2 > 10) is False since the x2 is less than 10, and the second expression (x2 < 5) is True since the x2 is less then 5. So frist expression is False, and secon is True and between those two expressions is logical operator
or that will generated True.
x2 > 10 or x2 < 5? The result is True
In the 5. code line the expression
x2 > 1 or x2 < 6 is tested and the result is printed out. The first expression (x2 > 1) is True since x2 is greater than 1, and the second expression (x2 < 6) is also True since x2 is less than 6. The first and second expressions are True so the output of
or logical operator is also True.
x2 > 1 or x2 < 6? The result is True
In the 6. code line the expression
x3 > 22 or x3 > 11 is tested and the result is printed out. The first expression (x3 > 22) is False since x3 is less than 22 and the second expression (x3 > 11) is also False since x3 is less then 11. The first and second expressions are False so the output of
or logical operator is also False.
x3 > 22 or x3 > 11? The result is False
In the 7. code line the expression
x3 > 9 or x3 < 11 is tested and the result is printed out. The first expression (x3 > 9) is True since x3 is greater than 9 and the second expression (x3 < 11) is also True since x3 is less than 11. Since both expressions are True the output will be True.
x3 > 9 or x3 < 11? The result is True
The logical operator
not will reverse result. The
not logical operator will return False if the result is True and vice versa. All cases of the logical operator
not are shown in Table 3.
Table 3 - possible outcomes of not operator.
not | Arugment | Output |
not | True | False |
not | False | True |
Example 3 Apply the
not operator to the followng expressions (x1 = 6, x2 = 3; x3 = 10).
- x1 < 5 and x3 == 10
- x1 > 1 or x2 < 4
- x2 > 1 and x3 > 5
- x2 < 5 or x2 < 1
Solution:First we need to create variables x1, x2, and x3 and assign the values 6, 3, and 10 to them, repsectively. Then each of the previous cases will be imported as argument of not logical operator i.e. not(x1 < 5 and x3 == 10). After that the print function will be created where the first term will be string of the case we are trying to solve followed by comma with the actual problem. For example: print("not(x1 < 5 and x3 == 10)? The result is",not(x1 < 5 and x3 == 10)). The entire code of this example can be written as:
x1 = 6; x2 = 3; x3 = 10
print("not(x1 < 5 and x3 == 10)? The result is",not(x1 < 5 and x3 == 10))
print("not(x1 > 1 or x2 < 4)? The result is",not(x1 > 1 or x2 < 4))
print("not(x2 > 1 and x3 > 5)? The result is",not(x2 > 1 and x3 > 5))
print("not(x2 < 5 or x2 < 1)? The result is",not(x2 < 5 or x2 < 1))
The output of previous code is given below.
not(x1 < 5 and x3 == 10)? The result is True
not(x1 > 1 or x2 < 4)? The result is False
not(x2 > 1 and x3 > 5)? The result is False
not(x2 < 5 or x2 < 1)? The result is False
In the 1. code line we have defined variables x1, x2 and x3 and assign the integer values in the following order: 6, 3, and 10, respectively.In the 2. code line we have tested the expression
not(x1 < 5 and x3 == 10) and printed out the result. In the 2. code line the first expression (x1 < 5) is Flase since x1 is greater than 5, and the second expression (x3 == 10) is True since x3 is equal to 10. Between first and second expression there is logical operator
and and the result of this expression (x1 < 5 and x3 == 10) is False since both expression must be True so that logical operator
and will produce True. However, the entire expression is the argument of the
not logical operator. The result of expression without
not is False and with
not is True.
not(x1 < 5 and x3 == 10)? The result is True
In the 3. code line we have tested the expression
not(x1 > 1 or x2 < 4) and printed out the result. The first expression (x1 > 1) in the 3. code line is True since x1 is greater than 1, and the second expression (x2 < 4) is True since x2 is less than 4. Between first and second expression there is logical operator
or which will in this case produce the output True since the output of both expressions is True (in case of
or only one expression must be True so that output would be True). Howver since the entire expression (x1 > 1 or x2 < 4) is arugmet of the
not logical operator the output will be False.
not(x1 > 1 or x2 < 4)? The result is False
In the 4. code line we have tested the expression
not(x2 > 1 and x3 > 5) and printed out the result. The first expression (x2 > 1) in the 4. code line is True since x2 is greater than 1 and the second expression (x3 > 5) is True since x3 is greater than 5. Between the first and second expression the logical operator
and is located and the output of the entire expression
x2 > 1 and x3 > 5 is True. The previous expression is the argument of the
not logical operator so the output is False.
not(x2 > 1 and x3 > 5)? The result is False
In the 5. code line we have tested the expression
not(x2 < 5 or x2 < 1) and printed out the result. The first expression (x2 < 5) is True since x2 is lower than 5 and the second expression (x2 < 1) is False since x2 is greater than 1. Between first and second expression there is logical operator
or located so the output of the expression
x2 < 5 or x2 < 1 is True. This entire expression is the arugment of the
not logical operator so the finla output is false.
not(x2 < 5 or x2 < 1)? The result is False