Now that we understand how to create conditions using comparison operators, it's time to learn how to combine these conditions to make even more sophisticated decisions. This is where logical operators come into play. They allow us to build complex expressions from simpler ones, leading to more nuanced and powerful program logic.
Python provides three primary logical operators: and, or, and not. Each has a distinct role in evaluating the truthiness of combined conditions.
The and operator is used when you need both conditions to be true for the entire expression to be true. Think of it as a requirement for everything to align. If even one of the conditions is false, the whole expression evaluates to false.
is_sunny = True
is_warm = True
if is_sunny and is_warm:
print("It's a perfect day for a picnic!")
else:
print("Maybe another day.")Let's see how and works with different scenarios:
True and Trueevaluates toTrue.True and Falseevaluates toFalse.False and Trueevaluates toFalse.False and Falseevaluates toFalse.
graph TD;
A[Condition 1];
B[Condition 2];
C{AND Operator};
D[Result is True ONLY IF both A and B are True];
A --> C;
B --> C;
C --> D;
The or operator, on the other hand, is more forgiving. The entire expression is true if at least one of the conditions is true. It only evaluates to false if both conditions are false. This is useful when you have alternative options or requirements.
has_coupon = False
is_member = True
if has_coupon or is_member:
print("You get a discount!")
else:
print("No discount for you this time.")Here's how or behaves:
True or Trueevaluates toTrue.True or Falseevaluates toTrue.False or Trueevaluates toTrue.False or Falseevaluates toFalse.
graph TD;
A[Condition 1];
B[Condition 2];
C{OR Operator};
D[Result is True IF either A or B (or both) are True];
A --> C;
B --> C;
C --> D;
The not operator is a unary operator, meaning it operates on a single condition. It simply inverts the truth value of the condition it's applied to. If a condition is true, not makes it false, and vice versa. This is handy for checking if something is not the case.
is_logged_in = False
if not is_logged_in:
print("Please log in to access this feature.")Examples of not:
not Trueevaluates toFalse.not Falseevaluates toTrue.
graph TD;
A[Condition];
B{NOT Operator};
C[Result is the opposite of the Condition's truth value];
A --> B;
B --> C;
It's important to remember operator precedence. Generally, not is evaluated first, then and, and finally or. However, you can use parentheses () to explicitly control the order of evaluation, making your code clearer and preventing unexpected results.
age = 25
income = 50000
is_student = False
# Without parentheses, 'and' might be evaluated before 'or'
# if not is_student and age > 18 or income > 60000:
# print("You might qualify for a loan.")
# With parentheses, the logic is clear
if not is_student and (age > 18 or income > 60000):
print("You might qualify for a loan.")
else:
print("Loan application denied.")Combining these logical operators allows you to build sophisticated decision-making structures in your Python programs. Mastering them is a crucial step towards writing more intelligent and responsive code.