Logical Operators in Python (and, or, not)

1 / 10

Logical operators are three words — and, or, and not — that help Python check multiple True/False (Boolean) conditions together.

What is the 'and' Operator?

The and operator in Python checks if the both conditions are true . If even one condition is false, the entire result will be False.

Suppose there are two conditions to drive a car:

  1. You must have a driving license
  2. Your age must be above 18 years

So in Python we will write this:

has_license = True
age = 22

can_drive = (age > 18) and (has_license == True)

print(f"Can this person drive a car? {can_drive}")

# Output: Can this person drive a car? True

Here both the conditions are true - age is above 18 and also has a license, so the output came True.

Now if we remove the license:

has_license = False
age = 22

can_drive = (age > 18) and (has_license == True)

print(f"Can this person drive? {can_drive}")

# Output: Can this person drive? False

Even though the age is correct, he doesn't have a license - so the result is False.

Behavior of the and Operator:

Condition 1andCondition 2Result
TrueandTrueTrue
TrueandFalseFalse
FalseandTrueFalse
FalseandFalseFalse

What is the 'or' Operator?

The or operator is a bit loose. Its job is to check any one of the two conditions is true.

Example:
Suppose: You get free shipping only if:

  • Your order is more than ₹50, or
  • You are a premium member.

Now look at the code:

order_total = 30
is_premium_member = True

gets_free_shipping = (order_total > 50) or (is_premium_member == True)

print(f"Gets free shipping? {gets_free_shipping}")

# Output: Gets free shipping? True

Behavior of the or Operator:

Condition 1orCondition 2Result
TrueorTrueTrue
TrueorFalseTrue
FalseorTrueTrue
FalseorFalseFalse

What is the 'not' Operator?

The not operator is the simplest in Python. Its job is to convert True to False and False to True.

Example:

Condition: The door will open only when the alarm is off (not active).

is_alarm_active = False # the alarm is off

can_open_door = not is_alarm_active # not False → True

print(f"Can the door open? {can_open_door}")

# Output: Can the door open? True

Here the value of is_alarm_active was False, and not changed it to True – meaning the door can open.

Behavior of the not Operator:

ConditionResult after applying not
TrueFalse
FalseTrue

Order of Operations in Python

When you write logical operators (not, and, or) together in a single line in Python, then Python has to decide which operator will run first.

Python's logic operator order is: not > and > or

Meaning Python will first check the not part, then and, then or.

But if you have used brackets, then Python will first check the thing inside the brackets, no matter what is in it.

Understand with a simple example:

Code without brackets:

result = True or False and False

Now how will Python understand this?

  1. First it will check False and False → this is False
  2. Then True or False will remain → this is True

So the result will be: True

But if you put brackets:

result = (True or False) and False

Now Python will first check the one inside the brackets:

  1. (True or False) → this is True
  2. Then True and False → this is False

So this time the result will be: False

Conclusion

Logical operators help Python make smart decisions:

  • Use and when everything is necessary to be true
  • Use or when someone calls true
  • Use not to reverse the result

Next Lesson: Identity and Membership Operators (is, in) in Python

Scroll to Top