Python Logical Operators (and, or, not)
What Are Logical Operators?
A logical operator is a keyword that connects two or more boolean expressions to produce a single True or False result. It helps you create complex conditions in if statements and while loops. This lets you write more precise and readable code. For example, you can check if a user is both an "admin" and is "active" before granting access.
Want to master Python? This lesson is part of our Free Python course covering operators, control flow, and data structures.
Explore this free Python course →1. The and Operator
The and operator returns True only when both conditions are true. If either condition is false, the entire expression becomes False. Use this operator when you need multiple conditions to be met at the same time.
Syntax and Examples
The and operator goes between two boolean values or expressions.
# Both are True -> True
result = True and True
print(result)
# One is False -> False
result = True and False
print(result)
# One is False -> False
result = False and True
print(result)
# Both are False -> False
result = False and False
print(result)
Your output will appear here...
Practical Use Case
A common use for and is checking if a value falls within a specific range. You can verify if a number is greater than a minimum and less than a maximum in a single line.
age = 25
if age >= 18 and age <= 65:
print("The person is of working age.")
else:
print("The person is not of working age.")
Your output will appear here...
2. The or Operator
The or operator returns True if at least one condition is true. It only returns False if both conditions are false. Use this operator when you need your code to proceed if any one of several conditions is met.
Syntax and Examples
Like and, the or operator goes between two boolean expressions.
# One is True -> True
result = True or True
print(result)
# One is True -> True
result = True or False
print(result)
# One is True -> True
result = False or True
print(result)
# Both are False -> False
result = False or False
print(result)
Your output will appear here...
Practical Use Case
You can use or to check for multiple acceptable conditions. For example, you might want code to run only on the weekend.
day = "Sunday"
if day == "Saturday" or day == "Sunday":
print("It's the weekend!")
else:
print("It's a weekday.")
Your output will appear here...
You're making great progress! Ready to practice with real-world problems?
Take Python Exercises3. The not Operator
Unlike and and or, the not operator works on a single expression. It flips the boolean value, turning True into False and False into True. It's useful for checking if a condition is absent.
Syntax and Examples
The not operator goes before the boolean expression you want to flip.
# Flips True to False
result = not True
print(result)
# Flips False to True
result = not False
print(result)
Your output will appear here...
Practical Use Case
The not operator often makes code more readable. Instead of writing an awkward comparison like is_logged_in == False, you can write a cleaner version.
is_logged_in = False
if not is_logged_in:
print("Please log in to continue.")
Your output will appear here...
Logical Operators Truth Table
| Operand A | Operand B | A and B |
A or B |
not A |
|---|---|---|---|---|
True |
True |
True |
True |
False |
True |
False |
False |
True |
False |
False |
True |
False |
True |
True |
False |
False |
False |
False |
True |
Combining Logical Operators
You can combine and, or, and not to build specific conditional logic. When you mix them, Python evaluates them in a specific order: not first, then and, and finally or.
The Importance of Parentheses
Use parentheses () to group expressions and control the order of evaluation. This overrides the default precedence and makes your logic clearer to other developers.
Consider this expression without parentheses, where and runs first:
result = True or False and False
print(result)
result = (True or False) and False
print(result)
Your output will appear here...
Short-Circuit Evaluation
Python's logical operators use short-circuit evaluation to work more efficiently. This means Python stops evaluating an expression as soon as it knows the final result.
- For
and: If the first expression isFalse, the overall result must beFalse, so Python skips the second expression. - For
or: If the first expression isTrue, the overall result must beTrue, so Python skips the second expression.
Why It Matters
Short-circuiting helps you write safer code that avoids errors. A common pattern is checking if an object exists before trying to access its attributes.
user = None
if user is not None and user.is_admin:
print("User is an admin.")
else:
print("Could not verify user.")
Your output will appear here...
Final Challenge: Access Control
Put your logical skills to the test. Create a permission check system.
Your Task:
- Create a variable
has_key(set toTrue). - Create a variable
knows_password(set toFalse). - Create a variable
is_banned(set toFalse). - Access is granted if a user is NOT banned AND (has a key OR knows the password).
has_key = True
knows_password = False
is_banned = False
access_granted = not is_banned and (has_key or knows_password)
print(access_granted)
Your output will appear here...
Lesson Completed
You have successfully learned about Python Logical Operators (and, or, not), short-circuit evaluation, and how to combine them for complex logic.
Full Python Course
Master Python with 11+ hours of content, 50+ exercises, and real-world projects.
Enroll Now