Logical operators let you combine conditions. Python has three of them: and, or and not. You will use them all the time inside if statements and while loops, for example to check that a user is over 18 and has an ID, or that a number is divisible by 3 or by 5.
Unlike C or Java, Python writes these operators as plain English words. &&, || and ! do not work in Python.
Python Logical Operators at a Glance
| Operator | Meaning | Example | Result |
|---|---|---|---|
and |
True only if both conditions are True | 10 < 20 and 4 > 0 |
True |
or |
True if at least one condition is True | 4 > 1 or 5 > 10 |
True |
not |
Reverses the result | not 5 > 3 |
False |
Truth Table for and, or
This small program prints every combination of two Boolean values:
for a in (True, False):
for b in (True, False):
print(a, b, a and b, a or b)
Output (columns: a, b, a and b, a or b):
True True True True
True False False True
False True False True
False False False False
Notice the pattern: and gives True in only one row, while or gives False in only one row. not needs just one value: not True is False and not False is True.
Example: Checking a Number
number = 26
print(number % 2 == 0 and number < 100)
print(number % 2 == 0 or number % 5 == 0)
print(not (number % 2 == 0))
Output:
True
True
False
26 is even and less than 100, so the first line is True. For or, one True side is enough. 26 is even, so the last line is not True, which is False. You can also read the number with number = int(input("Enter a number: ")) and try different values.
Example: A Simple Login Check
username = "riya"
password = "abc123"
if username == "riya" and password == "abc123":
print("Login successful")
else:
print("Wrong username or password")
Output:
Login successful
Both checks must pass, so and is the right operator here. If either one is wrong, the else part runs. (Real apps never store passwords like this; the example only shows how and works.)
Example: Leap Year with and / or
A year is a leap year if it is divisible by 4 but not by 100, or if it is divisible by 400. That rule needs both and and or:
for year in (1900, 2000, 2024, 2026):
leap = (year % 4 == 0 and year % 100 != 0) or year % 400 == 0
print(year, leap)
Output:
1900 False
2000 True
2024 True
2026 False
Short-Circuit Evaluation
Python stops checking as soon as it knows the answer:
- With
and, if the left side is False, the whole thing must be False, so the right side is never run. - With
or, if the left side is True, the whole thing must be True, so the right side is skipped.
def check():
print("check() was called")
return True
print(False and check())
print(True or check())
print(True and check())
Output:
False
True
check() was called
True
check() runs only in the third line. This is useful for safe checks such as if count != 0 and total / count > 50:. If count is 0, the division never runs, so there is no ZeroDivisionError.
and / or Return Values, Not Just True or False
This surprises many beginners. and and or return one of the actual values they were given:
a and breturnsaifais falsy, otherwiseb.a or breturnsaifais truthy, otherwiseb.
print(0 and 10)
print(7 and 10)
print(0 or 10)
print(7 or 10)
name = ""
print("Hello,", name or "Guest")
print(not 0, not "hello")
Output:
0
10
10
7
Hello, Guest
True False
In Python, 0, 0.0, an empty string "", an empty list [], an empty dictionary {} and None count as False. Almost everything else counts as True. That is why name or "Guest" is a common way to give a default value. not is different: it always returns True or False.
Precedence: not, then and, then or
When you mix them without brackets, Python runs not first, then and, then or. Comparisons such as == run before all three.
print(True or False and False)
print((True or False) and False)
print(not True == False)
Output:
True
False
True
The first line is read as True or (False and False). The last line is read as not (True == False), because == runs before not. Brackets make your intention clear, so use them whenever you mix operators.
Common Mistake: x == 1 or 2
choice = 3
print(choice == 1 or 2)
print(choice == 1 or choice == 2)
print(choice in (1, 2))
Output:
2
False
False
choice == 1 or 2 does not mean “choice is 1 or 2”. Python reads it as (choice == 1) or 2, which gives 2, a truthy value. So an if using it would always run. Write the full comparison on both sides, or use the membership operator in.
Practice Questions
print(not (3 > 2) or 5 == 5)print(4 > 2 and 0)print([] or [0])
Answers: 1) True. The left side is False but 5 == 5 is True. 2) 0. The left side is True, so and returns the right side, which is 0. 3) [0]. The empty list is falsy, so or returns the right side. A list containing 0 is not empty, so it is truthy.
Frequently Asked Questions
What are the three logical operators in Python?
and, or and not.
Can I use && and || in Python?
No. They are a syntax error in Python. & and | do exist, but they are bitwise operators, not logical ones.
What is short-circuit evaluation?
Python stops evaluating an and/or expression as soon as the result is known. The right side is skipped when the left side already decides the answer.
Which runs first, and or or?
and runs before or, and not runs before both.
Next: Python comparison operators · All operators: Python operators overview