Logical Operators in Python with Examples
Last Updated :
15 Apr, 2024
Operators are used to perform operations on values and variables. These are the special symbols that carry out arithmetic and logical computations. The value the operator operates on is known as the Operand.
In this article, we will discuss logical operators in Python definition and also look at some Python logical operators programs, to completely grasp the concept.
Logical operators in Python
In Python, Logical operators are used on conditional statements (either True or False). They perform Logical AND, Logical OR, and Logical NOT operations.
and |
Returns True if both the operands are true |
x and y |
x>7 and x>10 |
or |
Returns True if either of the operands is true |
x or y |
x<7 or x>15 |
not |
Returns True if the operand is false |
not x |
not(x>7 and x> 10) |
Truth Table for Logical Operators in Python
Truth Table for Python Logical Operators
Logical AND operator in Python
The logical AND operator returns True if both the operands are True else it returns False.
Logical AND operator in Python Examples:
Let’s look at some Python AND operator programs, and understand the workings of AND operator.
Example 1:
Python3
a = 10
b = 10
c = - 10
if a > 0 and b > 0 :
print ( "The numbers are greater than 0" )
if a > 0 and b > 0 and c > 0 :
print ( "The numbers are greater than 0" )
else :
print ( "Atleast one number is not greater than 0" )
|
Output
The numbers are greater than 0
Atleast one number is not greater than 0
Example 2:Â
Python3
a = 10
b = 12
c = 0
if a and b and c:
print ( "All the numbers have boolean value as True" )
else :
print ( "Atleast one number has boolean value as False" )
|
Output
Atleast one number has boolean value as False
Note: If the first expression is evaluated to be false while using the AND operator, then the further expressions are not evaluated.
Logical OR operator in Python
Logical OR operator returns True if either of the operands is True.
Logical OR operator in Python Examples
Let’s look at some Python OR operator program to understand it’s workingsÂ
Example 1:Â
Python3
a = 10
b = - 10
c = 0
if a > 0 or b > 0 :
print ( "Either of the number is greater than 0" )
else :
print ( "No number is greater than 0" )
if b > 0 or c > 0 :
print ( "Either of the number is greater than 0" )
else :
print ( "No number is greater than 0" )
|
Output
Either of the number is greater than 0
No number is greater than 0
 Example 2:Â
Python3
a = 10
b = 12
c = 0
if a or b or c:
print ( "Atleast one number has boolean value as True" )
else :
print ( "All the numbers have boolean value as False" )
|
Output
Atleast one number has boolean value as True
Note: If the first expression is evaluated to be True while using or operator, then the further expressions are not evaluated.
Logical NOT operator in Python
The logical not operator works with a single boolean value. If the boolean value is True it returns False and vice-versa.
Logical NOT operator in Python Examples
Let’s look at this Python NOT operator program to understand its working.Â
Example:Â
Python3
a = 10
if not a:
print ( "Boolean value of a is True" )
if not (a % 3 = = 0 or a % 5 = = 0 ):
print ( "10 is not divisible by either 3 or 5" )
else :
print ( "10 is divisible by either 3 or 5" )
|
Output
10 is divisible by either 3 or 5
Order of Precedence of Logical Operators
In the case of multiple operators, Python always evaluates the expression from left to right. We can verify Python logical operators precedence by the below example.Â
Example:Â
Python3
def order(x):
print ( "Method called for value:" , x)
return True if x > 0 else False
a = order
b = order
c = order
if a( - 1 ) or b( 5 ) or c( 10 ):
print ( "Atleast one of the number is positive" )
|
Output
Method called for value: -1
Method called for value: 5
Atleast one of the number is positive
Read Other Python Operators
Share your thoughts in the comments
Please Login to comment...