Python Logical Operators
In Python, Logical operators are used to test multiple conditions. It usually returns True or False. Logical Operators include and
, or
, not
.
Syntax
expression1 and expression2
expression1 or expression2
not expression
Example
# and operator
x = 5
print(x > 2 and x < 10)
# or operator
x = 5
print(x < 4 or x > 8)
# not operator
x = 5
print(not(x > 3 and x < 10))
Output
True
True
False
Explanation
and
operator returns true if both the statements are trueor
operator returns true if either of the statement is truenot
operator reverses the logical state of the operand
Use
Logical operators are used to test multiple conditions and perform logical operations. They are widely used in conditions and loops.
Important points
and
operator return True if all conditions are Trueor
operator returns True if at least one condition is Truenot
operator returns False if the condition is True
Summary
Logical operators are an important part of Python's conditional and looping statements. It helps in combining and evaluating multiple conditions, which makes the program more flexible and efficient.