In this tutorial, we’ll learn all three Python logical operators in detail. We’ll first see their role in Python programming. After that, we’ll use them inside Python code examples for better understanding.
Introduction: Python Logical Operators
Python operators are used to perform operations on its operands. Sometimes we want to test values with multiple conditions. That’s where the logical operators come into play. We’ve three logical operators in Python. Let’s now understand each of these operators with proper code examples.
3 Logical Operators In Python (Explained With Examples)
These 3 Python logical operators are listed below:
- Logical AND Operator (and)
- Logical OR Operator (or)
- Logical NOT Operator (not)
Logical AND Operator (and)
This operator return true if the Boolean value of both its operands return true or else it return false. See below example:
age=10 if age>0 and age< 11: # logically it is like- if true and true: print('age is between 1 to 10') else: print('age is not between 1 to 10')
Output
age is between 1 to 10
Both the conditions of if statement return true so its body got executed. Let’s now try to make one condition false and then see the result.
age=23 # code is same but in this one we've set the age to 23
Output
age is not between 1 to 10
We can see that else statement is executed as one condition of if statement return false. We know that for AND operator (and) to return true, both its operands should return true. In the second case, one condition return false which result in false returned by AND operator.
We can also use multiple AND operators. See below code:
age=23 if age>10 and age< 30 and age==23: # multiple and operators print('age is 23') else: print('age is not 23')
Output
age is 23
We can use AND operator multiple times as shown in the above example. It executed the body of if statement as all the conditions return true.
Logical OR Operator (or)
This operator return true if one of its operands return true, and false is returned if both of them results in false. See below code:
name='Zeeshan' if name=='Zeeshan' or name=='Yameen': # if true or false: print('Name is valid') else: print('Name is not valid')
Output
Name is valid
We can see that one condition results in false but still the body of if statement is executed which shows that if atleast one operand of OR operator return true then it results in true returned by OR operator.
We can use multiple OR operators as well. See below code:
val=4 if val>0 or val>20 or val>100: # if true or false or false: print('Value is valid') else: print('Value is not valid')
Output
Value is valid
We can see that only one operand return true which forces the OR operator to return true. See below examples as well.
print(5>10 or 5<4) # one condition is true print(5>2 or 5<8) # both the conditions are true print('Zee'=='Zee' or 'Zee'=='Shan' or 'Zee'=='Wazir') # one condition is true, other two are false
Output
False True True
Logical NOT Operator (not)
This operator is used to reverse the Boolean value of its single operand. Just make sure to use it on the left side of Boolean value. See below example:
val=True if not val: # if false: print('value is false') else: print('value is true')
Output
value is true
We can see that the value was initially true but specifying it with NOT operator converts it to false. As a result, the else statement got executed.
So this is how we can easily use the three Python logical operators. Feel free to share this post with your developer friends.
Don’t hesitate to ask if you have any questions related to the implementation of Python logical operators. We’ll be more than happy to answer all your questions.
Conclusion
To conclude this tutorial, hope you now have a detailed practical knowledge of how to use Python logical operators. We’ll be looking forward to have your valuable thoughts on this article.
Do visit our other posts on Python programming. Thank you for reading this article.