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:
- You must have a driving license
- 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 1 | and | Condition 2 | Result |
---|---|---|---|
True | and | True | True |
True | and | False | False |
False | and | True | False |
False | and | False | False |
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 1 | or | Condition 2 | Result |
---|---|---|---|
True | or | True | True |
True | or | False | True |
False | or | True | True |
False | or | False | False |
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:
Condition | Result after applying not |
---|---|
True | False |
False | True |
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?
- First it will check
False and False
→ this isFalse
- Then
True or False
will remain → this isTrue
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:
(True or False)
→ this isTrue
- Then
True and False
→ this isFalse
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