What does == mean in Python?
Short answer: == is the equality operator. It compares two values and returns True if they are equal and False if they are not: 5 == 5.0 is True and "5" == 5 is False. Do not confuse it with =, which assigns a value to a variable, or with is, which checks whether two names point to the same object.
You use == mostly inside conditions, such as if answer == "yes":, but it is an expression like any other: result = (a == b) stores True or False in result.
= stores a value, == compares values and is compares identity. Two lists with the same items are equal, but they are not the same object.
= vs == vs is at a glance
| Operator | What it does | Example | Result |
|---|---|---|---|
= | Assigns a value | x = 5 | x now holds 5 |
== | Compares values | x == 5 | True |
!= | Not equal | x != 3 | True |
is | Same object in memory | x is None | False |
is not | Not the same object | x is not None | True |
:= | Assigns inside an expression | if (n := len(items)) > 3: | Assigns and compares |
How == compares different types
Python compares the values, and some types count as equal to each other:
print(5 == 5.0) # True, int and float with the same value
print(True == 1) # True, bool is a kind of int
print("5" == 5) # False, a string is never equal to a number
print("a" == "A") # False, comparisons are case-sensitive
print([1, 2] == [1, 2]) # True, same items in the same order
print([1, 2] == [2, 1]) # False, order matters in lists
print({"a": 1, "b": 2} == {"b": 2, "a": 1}) # True, order does not matter in dicts
print((1, 2) == [1, 2]) # False, a tuple is not a listData that comes from input(), a file or an API is usually text, so convert it before comparing with numbers: int(answer) == 5.
== vs is
is checks identity: whether both names point to the exact same object. Two lists can be equal without being the same list:
a = [1, 2]
b = [1, 2]
c = a
print(a == b) # True, same content
print(a is b) # False, two different lists
print(a is c) # True, c is another name for aUse is only to compare with None, True or False as singletons, as in if result is None:. For numbers and strings, always use ==. If you write x is 1, Python warns you: SyntaxWarning: "is" with 'int' literal. Did you mean "=="?.
== vs = in a condition
A single = inside an if is a syntax error in Python, and the message points you to the fix:
if x = 5:
...
# SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?:=, the walrus operator, assigns a value inside an expression. It is useful in loops and conditions, but it is never a comparison.
Compare floats with math.isclose()
Floats are stored in binary, so some decimal results have tiny errors:
import math
print(0.1 + 0.2) # 0.30000000000000004
print(0.1 + 0.2 == 0.3) # False
print(math.isclose(0.1 + 0.2, 0.3)) # TrueUse math.isclose() to compare floats, or the decimal module when you need exact decimal arithmetic, such as with money. A special case: float("nan") == float("nan") is False; check it with math.isnan().
