Learn

Short lessons with real code you can edit and run on the page.

Logical Operators in Python

Lesson 11 Python 3.14 Runs in your browser Updated
In short

Python has three logical operators, and, or and not, written as words rather than the &&, || and ! of other languages. They combine or reverse conditions, most often in an if. and and or stop evaluating as soon as the answer is known and return one of their operands, the values on either side, while not always returns True or False.

Key facts

  • and is true only when both sides are true. or is true when at least one side is true. not flips a value.
  • && and || are syntax errors in Python. The words are the operators.
  • and and or short-circuit, so the right side never runs when the left side already decides the result.
  • Precedence is not, then and, then or. Parentheses make the intent visible.

What are the three logical operators in Python?

and, or and not. The first two combine two conditions and the third reverses one. The table shows every result.

Truth table for and, or and not in Python
aba and ba or bnot a
TrueTrueTrueTrueFalse
TrueFalseFalseTrueFalse
FalseTrueFalseTrueTrue
FalseFalseFalseFalseTrue
Python
print(True and False)
print(True or False)
print(not True)
Output
False
True
False

How do I use and, or and not in an if statement?

Put the whole combined condition after if. Each side is a comparison or a boolean, and the operator joins them into one answer.

Python
age = 25
has_ticket = True
if age >= 18 and has_ticket:
    print("Welcome in")
day = "sat"
if day == "sat" or day == "sun":
    print("Weekend")
if not has_ticket:
    print("No entry")
else:
    print("Ticket found")
Output
Welcome in
Weekend
Ticket found

Does Python use && or and?

Python uses the words. && and || come from C and JavaScript, and Python rejects them at the second character. A single & or | exists, but those are bitwise operators, not the logical ones.

Python
a = True
b = False
print(a && b)
Output
  File "main.py", line 3
    print(a && b)
             ^
SyntaxError: invalid syntax

What is short-circuit evaluation?

Python stops evaluating a condition as soon as the result is known. For and, a false left side settles it, so the right side never runs. For or, a true left side settles it. In this example the right side would divide by zero, and Python never reaches it.

Python
x = 0
print(x != 0 and 10 / x > 1)
print(x == 0 or 10 / x > 1)
Output
False
True

To guard a risky operation, put the check on the left of and and the operation on the right.

What do and and or actually return?

One of their operands, the values on either side of the operator, not necessarily a boolean. or returns the first truthy value it meets, or the last value if none is truthy. and returns the first falsy value, or the last value if all are truthy. Inside an if you never notice. Outside one, or gives you a one-line default.

Python
print(0 or "default")
print("first" or "second")
print("first" and "second")
print(0 and "never")
name = ""
print(name or "Anonymous")
Output
default
first
second
0
Anonymous

name or "Anonymous" keeps the name when there is one and falls back to "Anonymous" when it is empty. The fallback applies to any falsy value, so count or 10 also replaces a real 0.

What is the precedence of not, and and or?

Precedence is the order in which Python applies operators. Of the three, not binds tightest, then and, then or.

So not a or b means (not a) or b, and a or b and c means a or (b and c). Comparisons such as == bind tighter still, so not a == b means not (a == b). When a condition mixes them, add the parentheses even though Python doesn't need them.

Python
print(not True or True)
print(not (True or True))
print(True or False and False)
print((True or False) and False)
Output
True
False
True
False

How do I test membership or use exclusive or?

in and not in test whether a value is inside a string or collection. They replace long or chains. Python has no xor keyword. For two booleans, a != b is True when exactly one of them is true, and the ^ operator gives the same result.

Python
a = True
b = True
print(a != b)
print(a ^ b)
print("a" in "cat", "z" not in "cat")
Output
False
False
True True

Common mistakes with logical operators in Python

Three of these mistakes with logical operators raise no error. The first reads like correct code, so it is the hardest to spot.

  • x == 1 or 2 always counts as true. Python groups it as (x == 1) or 2, and 2 is truthy. Write x == 1 or x == 2, or x in (1, 2).
  • SyntaxError: invalid syntax on &&. Use and. Likewise, write or instead of || and not instead of !.
  • Mixing and and or without parentheses. It runs, and it may not mean what you read. Group the parts.
  • Expecting or to give True or False. It gives one of the operands. Wrap it in bool() when you need a boolean value to store.
Python
x = 5
print(x == 1 or 2)
print(x == 1 or x == 2)
print(x in (1, 2))
Output
2
False
False

Exercise

Complete the two print lines. The first prints whether the person may enter, which needs both an age of at least 18 and a ticket, so True. The second prints the opposite of has_ticket, so False.

Python
age = 25
has_ticket = True

print()
print()
Output
Show the solution
age = 25
has_ticket = True

print(age >= 18 and has_ticket)
print(not has_ticket)

Quiz

This quiz has 5 questions. Pick an answer to see why it is right or wrong.

  1. 1What does this program print?

    print(False or 7)
  2. 2What does this program print?

    x = 3
    print(x == 1 or 2)
  3. 3Which is the Python equivalent of && in other languages?

  4. 4What does this program print?

    print(not False and False)
  5. 5When this code runs, what happens to the division 10 / x?

    x = 0
    result = x != 0 and 10 / x > 1

Frequently asked questions

Does Python use && or and?
Python uses the words and, or and not. && and || are syntax errors, and a single & or | is a bitwise operator, not a logical one.
Is == a logical operator in Python?
No. == is a comparison operator that produces a boolean. The logical operators and, or and not combine or reverse conditions, based on whether each value counts as true or false.
What are the three logical operators in Python?
and, which is true when both sides are true; or, which is true when at least one side is true; and not, which reverses a value.
What is the order of precedence of not, and and or in Python?
not first, then and, then or. So not a or b means (not a) or b, and a or b and c means a or (b and c).
What does or return in Python?
The first operand that counts as true, or the last operand if none does. That is why name or "Anonymous" gives a default value.
Is there an XOR operator in Python?
Yes, ^, though there is no xor keyword. On integers it works bit by bit, and for two booleans a ^ b is True when exactly one of them is True, the same as a != b.