Menu Close

Python Logical Operators

Hi Python Programmer, In this guide, we are going to learn all about Python logical operators with the help of the example. In the previous tutorial, we have seen all about Python arithmetic, comparison, and assignment operators with the help of examples.

Logical operators are very useful when you are working with a real-life Python project because with the help of Python logical operators you can make multiple conditions on the basis of your requirements.

Python logical operators

Python logical operators are used on conditional statements ( either True or False ). There are three types of logical operators available in Python: Logical AND, Logical OR, Logical NOT.

Logical Operators in Python

OperatorDescriptionExample
Logical ANDReturns True if both operands are Truex and y
Logical ORReturns True if one of the operands is Truex or y
Logical NOTReturns True if the operand is Falsenot x

Python Logical AND Operator

Python logical AND operator is used to return True if both of the operands are True.

Example


a = 12
b = 30
c = -10

if a > 10 and b < 50:
	print(f"{a} is greater than 10 and {b} is less than 50")
if c > 10 and b < 50:
    print(f"Something went wrong..")
    
    
# Output
# 12 is greater than 10 and 30 is less than 50

Example 2


a = 12
b = 30
c = -10

if a and b and c:
	print(f"All the variables have value")
else:
    print(f"Something went wrong..")
    
# Output
# All the variables have value

Logical OR Operator

Python logical OR operator is used to return True if one of the operand is True.

Example 1:


a = 12
b = 30

if a < 0 or b > 10:
	print(f"Atleast one operand is True")
else:
	print("No operand is True")
    
# Output
 # Atleast one operand is True

Example 2:


a = 1
b = 0
c = 0

if a or b or c:
	print(f"At least one operand is True")
else:
    print(f"Something went wrong..")
    
# Output
# At least one operand is True

Logical NOT Operator

Python logical NOT operator is only working with a single value or variables. It returns True if the value is False and vice-versa.

Example


a = 0
if a:
    print("a has True value")

if not a:
    print("Condition is False.")
    
# Output
# Condition is False.

Conclusion

So, here we have seen all about Python logical operators with the help of examples. Logical operators in Python are going to be very helpful when you work with condition statements and you want to check multiple conditions simultaneously.

If you are a Python developer, Then you can’t ignore operators in Python because operators ease the perform the operators on variables and values.

I hope this article, will have helped you. if you like this article please share and support us.

Python Operators


Thanks for reading

Python Bitwise Operators
Data Types in Python

Related Posts