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) # Output: True # One is False -> False result = True and False print(result) # Output: False # One is False -> False result = False and True print(result) # Output: False # Both are False -> False result = False and False print(result) # Output: False
# 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 # Check if the age is between 18 and 65 if age >= 18 and age <= 65: print("The person is of working age.") else: print("The person is not of working age.")
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) # Output: True # One is True -> True result = True or False print(result) # Output: True # One is True -> True result = False or True print(result) # Output: True # Both are False -> False result = False or False print(result) # Output: False
# 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" # Check if it's a weekend if day == "Saturday" or day == "Sunday": print("It's the weekend!") else: print("It's a weekday.")
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 Exercises

3. 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) # Output: False # Flips False to True result = not False print(result) # Output: True
# 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 # Check if the user is not logged in if not is_logged_in: print("Please log in to continue.")
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 # 1. `and` is evaluated: False and False -> False # 2. `or` is evaluated: True or False -> True print("Result without parentheses:", result) # Output: True # Now see how parentheses change the outcome by forcing or to run first: result_with_parens = (True or False) and False # 1. Parentheses are evaluated: True or False -> True # 2. `and` is evaluated: True and False -> False print("Result with parentheses:", result_with_parens) # Output: False
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 is False, the overall result must be False, so Python skips the second expression.
  • For or: If the first expression is True, the overall result must be True, 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 # This line would cause an error because `user` is None # if user.is_admin and user.has_permissions: # This is safe due to short-circuiting # Because `user is not None` is False, the second part is never checked. if user is not None and user.is_admin: print("User is an admin.") else: print("Could not verify user.")
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:

  1. Create a variable has_key (set to True).
  2. Create a variable knows_password (set to False).
  3. Create a variable is_banned (set to False).
  4. Access is granted if a user is NOT banned AND (has a key OR knows the password).
# TODO: Create variables # has_key = ... # knows_password = ... # is_banned = ... # TODO: Check access # access_granted = ... # print(access_granted)
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
📝

Next Lesson

Continue with the next lesson on Python Control Flow.

Next Lesson ->

Scroll to Top