Python Logical Operators with Examples
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 Operand.
Table of Content
In Python, Logical operators are used on conditional statements (either True or False). They perform Logical AND, Logical OR and Logical NOT operations.
|and||Logical AND: True if both the operands are true||x and y|
|or||Logical OR: True if either of the operands is true||x or y|
|not||Logical NOT: True if operand is false||not x|
Logical AND operator
Logical operator returns
True if both the operands are True else it returns
The numbers are greater than 0 Atleast one number is not greater than 0
Atleast one number has boolean value as False
Note: If the first expression evaluated to be false while using and operator, then the further expressions are not evaluated.
Logical or operator
Logical or operator returns True if either of the operands is True.
Either of the number is greater than 0 No number is greater than 0
Atleast one number has boolean value as True
Note: If the first expression evaluated to be True while using or operator, then the further expressions are not evaluated.
Logical not operator
Logical not operator work with the single boolean value. If the boolean value is
True it returns
False and vice-versa.
10 is divisible by either 3 or 5
Order of evaluation of logical operators
In the case of multiple operators, Python always evaluates the expression from left to right. This can be verified by the below example.
Method called for value: -1 Method called for value: 5 Atleast one of the number is positive
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course