Python Logical Operators (AND, OR, NOT) – Complete Guide with Examples

Have you ever wondered how a program makes complex decisions, like whether to approve a loan or grant access to a secure system? As a beginner, you might know how to check a single condition with an if statement, but real-world problems often depend on multiple factors. How do you tell Python to consider two or more conditions at once?

The answer lies in Python logical operators. These powerful tools are the building blocks of decision-making in code. By the end of this guide, you will confidently use the three logical operators—andor, and not—to combine conditions and write more intelligent, flexible Python programs.

What Are Python Logical Operators?

In simple terms, logical operators allow you to combine multiple conditional statements. They evaluate all the given conditions and return a single Boolean value: either True or False.

Think of it like a bank deciding on a loan application. The bank doesn’t just check one thing. It might have two main rules:

  1. The applicant’s age must be over 18.
  2. The applicant’s income must be above a certain threshold.

The bank needs to check both conditions together. This is exactly what logical operators like and are for! They help your program make these multi-faceted decisions.

The Three Core Logical Operators: AND, OR, NOT

Let’s break down each of the three operators with simple explanations and practical code examples.

The and Operator: Ensuring All Conditions Are Met

The and operator is strict. It returns True only if all the conditions being checked are True. If even one condition is False, the entire expression becomes False.

It works like a series of security checkpoints; you must pass through all of them to proceed.

Syntax:

condition1 and condition2

Code Example:
Let’s see the and operator in action in a Python shell.

# Both conditions are True
print(5 > 4 and 4 < 5)  # Output: True

# One condition is False
print(5 > 4 and 4 > 5)  # Output: False

# Both conditions are False
print(5 < 4 and 4 > 5)  # Output: False

In the first example, 5 > 4 is True and 4 < 5 is also True, so True and True results in True. In the other examples, the presence of at least one False causes the entire expression to evaluate to False.

The or Operator: Needing Just One Condition

The or operator is more flexible. It returns True if at least one of the conditions is True. It only returns False if every single condition is False.

Imagine a discount policy: “You get a discount if you are a student OR a senior citizen.” Meeting just one of these criteria is enough.

Syntax:

condition1 or condition2

Code Example:
Let’s test out the or operator.

# Both conditions are True
print(4 < 5 or 5 > 4)   # Output: True

# One condition is True, one is False
print(4 < 5 or 5 < 4)   # Output: True

# Both conditions are False
print(5 > 5 or 4 > 5)   # Output: False

As you can see, the result is True whenever there is at least one True value. It only gives False when both sides are False.

The not Operator: Reversing the Logic

The not operator is a unary operator, meaning it only works on one condition. Its job is simple: it reverses the Boolean value. If a condition is Truenot makes it False, and vice-versa.

Think of it as an “opposite day” switch for your conditions.

Syntax

not condition

Code Example:
See how not flips the result.

# not True becomes False
print(not (5 > 4))  # Output: False

# not False becomes True
print(not (5 < 4))  # Output: True

# You can also use it directly with Boolean values
print(not True)     # Output: False
print(not False)    # Output: True

Important Note: The not operator is applied to a single condition. Trying to use it between two conditions like condition1 not condition2 will result in a syntax error.

Putting It All Together: A Practical Example

Let’s create a small program that uses these operators to simulate a real-world scenario: checking loan eligibility based on age and income.

# Loan Eligibility Checker
applicant_age = 25
applicant_income = 45000
required_income = 40000

# Check if applicant is over 18 AND has an income above the required amount
is_eligible = (applicant_age > 18) and (applicant_income >= required_income)

print(f"Applicant eligible for loan: {is_eligible}")
# Output: Applicant eligible for loan: True

# Let's test with a different age
applicant_age = 17
is_eligible = (applicant_age > 18) and (applicant_income >= required_income)
print(f"Applicant eligible for loan: {is_eligible}")
# Output: Applicant eligible for loan: False

This example clearly shows how the and operator combines two crucial checks into one decisive result.

Ready to Go from Basics to Pro?

Mastering fundamental concepts like logical operators is a huge step, but it’s just the beginning of your Python journey. To truly become proficient, you need a structured learning path that covers everything from core syntax to building real-world applications. Our comprehensive course is designed to take you from a beginner to a confident Python developer with expert guidance and hands-on projects.

If you’re serious about mastering Python, check out our pre-recorded python comprehensive video course.

Conclusion

You’ve now unlocked a key component of programming logic in Python. The logical operators andor, and not are essential for controlling the flow of your programs and making them smarter. Remember: and requires all conditions to be true, or requires at least one to be true, and not simply inverts a single condition. The best way to solidify this knowledge is to open your code editor and start experimenting with these operators in your own if statements. Keep practicing, and you’ll be combining conditions like a pro in no time!

Common Questions About Python Logical Operators

What’s the difference between logical and boolean operators?
In Python, they are essentially the same thing. The operators andor, and not are called “logical operators” because they perform logical operations. They are also called “boolean operators” because they operate on and return Boolean values (True or False).

Can I use more than two conditions with and or or?
Absolutely! You can chain multiple conditions together. For example, condition1 and condition2 and condition3. The same rules apply; and will be True only if all conditions are True, and or will be True if at least one condition is True.

What is the order of precedence for these operators?
Python evaluates operators in a specific order: not is evaluated first, then and, and finally or. You can use parentheses () to change this order and make your intentions clear, which is always a good practice for writing readable code.