Logical Operators
-
Python logical operators (
and,or,not) are keywords used to evaluate multiple conditions and return a Boolean result (TrueorFalse). These operators help control the flow of your program by allowing multiple conditions to be combined inif,elif, orwhilestatements.
Logical operators in Python are used to combine multiple conditions or invert a condition. They return Boolean values: True or False.
Python provides three logical operators:
Logical operators are commonly used in decision-making, loops, and complex conditions.
Logical Operators – Truth Table
1. AND Operator (and)
Returns True only if both conditions are True.
2. OR Operator (or)
Returns True if at least one condition is True.
3. NOT Operator (not)
Reverses the Boolean value.
Logical Operators Example
Demonstrates the use of and, or, and not operators with two conditions.
# Python program to demonstrate logical operators
a = 10
b = 5
c = 8
# Using 'and' operator
print("(a > b) and (b < c):", (a > b) and (b < c)) # True because both conditions are True
# Using 'or' operator
print("(a < b) or (b < c):", (a < b) or (b < c)) # True because one condition is True
# Using 'not' operator
print("not(a > b):", not(a > b)) # False because a > b is True, 'not' reverses it