Logical Operators: 'and' for Conjunctions
From the csc241\ curriculum
TL;DR
The and operator combines two conditions, evaluating to True only if both conditions are True. Think of it like needing two things to be true for a whole statement to be true. It's super useful for checking multiple requirements simultaneously in your code.
1. The Mental Model
Imagine you're trying to get a discount: you need to be a student and have a coupon. If either of those isn't true, you don't get the discount. That's exactly how the and operator works.
2. The Core Material
In programming, a logical operator helps you combine or modify Boolean (True/False) expressions. The and operator is one of the most common, used to create conjunctions.
When you use condition1 and condition2, the entire expression will be True if, and only if, condition1 is True and condition2 is True. If even one of them is False, the entire expression becomes False.
Let's look at the truth table for and:
| Condition 1 | Condition 2 | Result (Condition 1 and Condition 2) |
|---|---|---|
| True | True | True |
| True | False | False |
| False | True | False |
| False | False | False |
Short-Circuiting with and

Photo by Júlio Riccó on Pexels
A cool feature of and (and or) in many languages (like Python) is short-circuiting. If the first condition in an and expression is False, the interpreter knows the whole expression must be False already, so it doesn't even bother evaluating the second condition. This can save computation time and prevent errors if the second condition would cause one (e.g., dividing by zero).
graph TD
A["Start: Evaluate Condition 1"] --> B{Condition 1 is True?};
B -- "Yes" --> C["Evaluate Condition 2"];
C --> D{Condition 2 is True?};
D -- "Yes" --> E["Result: True"];
B -- "No" --> F["Result: False (Short-circuited)"];
D -- "No" --> F;
Using and in Python

Photo by cottonbro studio on Pexels
In Python, you'd use and directly:
# Example 1: Both True
age = 25
has_license = True
can_drive = (age >= 18) and has_license
print(f"Can drive (age >= 18 and has_license): {can_drive}") # Output: True
# Example 2: One False
is_member = True
has_coupon = False
get_discount = is_member and has_coupon
print(f"Get discount (is_member and has_coupon): {get_discount}") # Output: False
# Example 3: Both False
is_raining = False
is_cold = False
go_outside = is_raining and is_cold
print(f"Go outside (is_raining and is_cold): {go_outside}") # Output: False
# Example 4: Short-circuiting in action
# (The second part (10 / 0) won't even be evaluated because 0 == 1 is False)
safe_calculation = (0 == 1) and (10 / 0)
print(f"Safe calculation: {safe_calculation}") # Output: False
# If it wasn't short-circuited, this would raise a ZeroDivisionError
3. Worked Example
Let's say you're building a simple login system. A user should only be granted access if their username is "admin" and their password is "password123".
# User inputs (for demonstration, normally you'd get these from user input)
user_entered_username = "admin"
user_entered_password = "password123"
# Define the correct credentials
correct_username = "admin"
correct_password = "password123"
# Check if both conditions are true using 'and'
is_username_correct = (user_entered_username == correct_username)
is_password_correct = (user_entered_password == correct_password)
access_granted = is_username_correct and is_password_correct
print(f"Is username correct? {is_username_correct}")
print(f"Is password correct? {is_password_correct}")
print(f"Access granted: {access_granted}")
# Let's try with incorrect password
user_entered_username_bad = "admin"
user_entered_password_bad = "wrongpass"
access_granted_bad = (user_entered_username_bad == correct_username) and \
(user_entered_password_bad == correct_password)
print(f"\nAccess granted (wrong password): {access_granted_bad}")
In the first scenario, both is_username_correct and is_password_correct are True, so access_granted becomes True. In the second scenario, user_entered_password_bad == correct_password is False, making access_granted_bad False as well, even though the username was correct.
4. Key Takeaways
- The
andoperator requires all combined conditions to beTruefor the entire expression to beTrue. - If any condition linked by
andisFalse, the entire expression evaluates toFalse. andis crucial for checking multiple criteria that all must be met.- Many languages use short-circuiting, meaning evaluation stops as soon as a
Falsecondition is found withand. - You can chain multiple
andconditions, likecond1 and cond2 and cond3.
Common Mistakes to Avoid

Photo by KATRIN BOLOVTSOVA on Pexels
- Confusing
andwithor:oris true if any condition is true,andneeds all to be true. - Forgetting parentheses:
a > 5 and b < 10is fine, but for complex conditions,(a > 5) and (b < 10)can improve readability and prevent unexpected operator precedence issues. - Assuming conditions are always evaluated: remember short-circuiting means some parts might be skipped.
- Using
&instead ofandfor logical operations in Python:&is a bitwise operator and works differently.
5. Now Try It
Write a Python program that asks the user for their age and if they have a valid ID (you can ask for "yes" or "no"). Then, use the and operator to determine if they are old enough to buy alcohol (age 21 or over) and have a valid ID. Print a message saying whether they can "Buy alcohol" or "Cannot buy alcohol".
Frequently asked about Logical Operators: 'and' for Conjunctions
More from csc241\
Get the full csc241\ curriculum
Clone the complete plan to your dashboard for unlimited AI-generated notes, practice quizzes, and a personalised revision schedule.
Save this course free