Logical operators in Python are binary operators which are used to combine two or more simple conditions or relational expressions.
In other words, logical operators combine one or more comparison into one condition group.
It is useful when we want to check more than one condition at a time and use the result.
A simple example of a logical operator that combines two relational expressions, or conditions, is as:
x > y and y > z # Two relational expressions combined by a logical and operator.
This kind of expression is called logical expression or compound relational expression.
The result of a logical operator is always boolean value, either true or false, according to the result of logical expression.
Since the result of logical operators is always boolean value after evaluation, the logical operators are also called boolean operators in Python.
With the help of Boolean operators, we can perform logical operations. We can create a powerful data testing statement by joining logical operators with relational or comparison operators.
Types of Logical or Boolean Operators in Python
In the Python language, there are three types of logical operators. We have listed them in the below table:
Operator | Description |
---|---|
and | Logical AND |
or | Logical OR |
not | Logical NOT |
Let us have a look at each logical operator one by one with the help of examples.
Logical AND Operator (and) in Python
The logical AND operator combines two relational expressions (or conditions) together into one condition group. It uses ‘and’ as a symbol.
The Python interpreter evaluates separately both expressions and then the logical and operator compares the result of both.
If the conditions on both sides of logical and operator are true, the operator returns true. If one or both conditions on either side of the operator are false, then it returns false.
Let’s take some examples in which we have combined two relational expressions by logical and operator.
print(20 > 10 and 30 < 40) # True. print(10 == 10 and 88 >= 78) # True. print(10 == 20 and 50 <= 40) # False.
From the above statements, it is clear that the logical and operator returns true only if all the expressions (or operands) are true. Otherwise, it returns false value.
Example Program based on Logical AND Operator
Let us take some example programs in which we will perform some operations based on logical and operator in Python.
Example 1:
x, y = 10, 5 result = (x == 10 and y == 5) print(result) result = (x == 10 and y > x) print(result) result = (x < y and y > x) print(result)
Output: True False False
Explanation:
a) In the first result statement, there are two conditions or relational expressions; (x == 10) and (y == 5). The result of both conditions are true. Therefore, the logical and operator returns true value.
b) In the second result statement, the condition (x == 10) is true, but the condition (y > x) is false. Therefore, the logical and operator returns false value.
c) In the third result statement, both conditions (x < y) and (y > x) are false. Therefore, the logical and operator returns false value.
Example 2
x, y, z = 20, 10, 25 # Use of logical and operator in the if statement. if(x > y and y > z): print("Hello") if(z > y and y < x): print("Python") if((y+200) < x and (y+150) < z): print("Hello Python")
Output: Python
Explanation:
a) In the first if statement, there are two conditions: x > y and y > z. The condition x > y is true, but y > z is not true. Therefore, the statement “Hello” is not displayed on the console.
b) In the second if statement, both conditions z > y and y < x are true. Therefore, the statement “Python” is displayed on the console.
c) In the third if statement, both conditions are false. Therefore, the statement “Hello Python” is also not displayed.
Example 3:
x = (15 < 20) and ("pen" < "pencil") y = ("Big" < "bigger") and (True != 1) z = ("A" <= "A") and ("a" != "a") print("x: ", x) print("y: ", y) print("z: ", z)
Output: x: True y: False z: False
Explanation:
a) In the preceding example code, x returns true because comparison on both sides is true. 15 is less than 20 and the string pen is less than pencil on the basis of ASCII values.
b) Similarly, y returns false because the condition (“Big” < “bigger”) is true, but the condition (True != 1) is false.
c) While, z returns false because the comparison on the right side is false.
Try it yourself
Example 4:
p = True and True q = False and True r = 'a' and 'b' s = False and 'a' t = 'a' and True print("p: ", p) print("q: ", q) print("r: ", r) print("s: ", s) print("t: ", t)
Example 5:
p = (True == 1) and (False == 0) q = ("2" != 2) and (2 == 2) r = ("5+5" == 5+5) and True s = (False == 0) and (5 > 2) t = (10 + True >= 10) and False print("p: ", p) print("q: ", q) print("r: ", r) print("s: ", s) print("t: ", t)
Logical OR Operator (or) in Python
The logical or operator in Python combines two or more expressions or conditions together into a single condition group.
The or operator returns true if one or both of the conditions returns true. If the conditions on both sides of the operator are false, the logical or operator returns false.
Here are some simple examples in which we have combined two conditions by logical or operator.
print((2 == 2) or (3 > 5)) # It will return true because comparison on the left side is true. print((5 > 18) or (3 != 9)) # It will return true because comparison on the right side is true. print((4 == 4) or (5 < 9)) # It will return true because comparison on both sides is true. print((4 < 2) or (2 == 1)) # It will return false because both comparisons are false. print((3 != 3) or (3 >= 9)) # It will return false because both comparisons are true.
From the above example codes, it is clear the logical or operator returns true only if at least one expression (or operand) is true.
Example Program based on Logical OR Operator
Let’s take some example programs in which we will perform some operations based on the logical or operator in Python.
Example 1:
x, y, z = 20, 10, 5 if(x > y or y > z): print("Python") if(z > y or y < x): print("JavaScript") if((y+20) < x or (z+15) < y): print("Java")
Output: Python JavaScript
Explanation:
a) In the first if statement, both conditions are true. Therefore, the interpreter displays ‘Python’ on the console.
b) In the second if statement, one condition on the right side of or operator is true. Therefore, the interpreter displays ‘JavaScript’ on the console.
c) In the third if statement, both conditions are false. Therefore, the interpreter does not display ‘Java’ on the console.
Example 2:
x, y, z = 10, 5, 20 if((x > y) or ( y == z)): print("One") if((x == y) or (y < z )): print("Two") if((x != y) or (y != z)): print("Three") if((x < y) or (y > z)): print("Four")
Output: One Two Three
In the preceding code, both conditions are false in the last if statement. Therefore, “four” is not displayed on the console.
Example 3:
a = ("big " != "bigger") or False print("a: ", a) b = True or False print("b: ", b) c = "a" or D print("c: ", c) d = ("ABc" > "abC") or False print("d: ", d)
Output: a: True b: True c: a d: False
Try It Yourself
Example 4:
p = (True == 1) or (False == 0) q = ("2" != 2) or (2 == 2) r = ("5+5" == 5+5) or False s = (False == 0) or (5 > 2) t = (10 >= True) or False print("p: ", p) print("q: ", q) print("r: ", r) print("s: ", s) print("t: ", t)
Logical NOT Operator (not)
The logical not operator is something different from the logical ‘and’ and ‘or’ operators. We use it for a single comparison. It is mainly used to reverse the result.
That is, if the condition evaluates true, the result of the condition returns false. If the expression evaluates false, the result of the condition returns true. For example:
if(not(2 > 5)): print("I love Python Programming")
In the above statement, (2 is greater than 5) is false, but the logical not operator reverse the result and makes it true. Therefore, the next statement will display “I love Python Programming” on the console.
Let’s understand the not operator with the help of some more examples.
print(not(5 == 5)) # It will return false because 5 is equal to 5 is true, but not operator makes it false. print(not(False)) # It will return true because not operator reverses the false into true. print(not(True)) # It will return false because not operator makes it false.
Example Program based on Logical NOT Operator
Let’s take some example programs in which we will perform some operations based on NOT operator.
Example 1:
a = not("ABc" > "ABC") or False print("a: ", a) b = not(True) or not(False) print("b: ", b) c = (True >= 0) or not(20 == 20) print("c: ", c) d = not("5" == 10) and not(False and (10 != 20)) print("d: ", d)
Output: a: False b: True c: True d: True
Try It Yourself
Example 2:
a = not("abc" < "ABCD") and not(False) print("a: ", a) b = not True and not False print("b: ", b) c = not("20" == 20) and not(True == False) print("c: ", c)
In this example, you learned types of boolean or logical operators in Python language with the help of various examples. Hope that you will have understood the basic points of logical operators and practiced all example programs.
Thanks for reading!!!
Next ⇒ Bitwise Operators in Python⇐ Prev Next ⇒