Python logical operators
This tutorial is a Python logical operators. python Logical operators are used to comparing multiple relational expressions. We can use logical operators by using that we can combine more than one relational expression and according to return true or false.
Python buil-in 3 logical operators:
- And
- Or
- Not
And
And an operator is more than one relational expression and it will return true. if one of the expressions is false then it returns false.
Syntax
expression1 and expression2
Example
numbers = 105 if numbers > 100 and numbers < 150: print("Number is between 100 and 150") print("Number is :",numbers) Output: Number is between 100 and 150 Number is : 105
Or
Or an operator is more than one relational expression and it returns true. if one of the expressions is true and the second false then it will return true.
Syntax
expression1 or expression2
Example
numbers = 105 if numbers == 100 or numbers == 105: print("Or operator success!") print("Number is", numbers) Output: Or an operator success! Number is 105
Not
not an operator is used to reverse the logical state of the expression.
Syntax
not expression1
Example
numbers = 150 if not numbers == 100: print("Not operator success!") print("Numbers is",numbers) Output: Not operator success! Numbers is 150