==

Value equality: do these hold the same data? (Identity — same object — is the separate `is`.)

Comparison operatorPython 1.0+Live demo
Common call
a == b
Returns
bool; unrelated types → False, never an error
Replaces
1 == 1.0 == True — numeric types cross-compare
Watch out
= assigns, == compares — inside if, = is a SyntaxError
aa — Left operand.type: Any · required == bb — Right operand.type: Any · required
→ bool

Demo

Live evaluation
Try:
Inputs
afloatleft
bfloatright
Output
5 == 5
True

Numbers compare by value across int/float. The float-trap case shows why computed floats should not be ==-compared: 0.1 + 0.2 does not equal 0.3.

Operands

NameTypeRequiredDescription
aAnyyesLeft operand.
bAnyyesRight operand.

Return value

bool — True when the operands compare equal by VALUE. Mixed unrelated types are simply unequal — no exception.

Common patterns

Container equality is element-wise
Lists, dicts and sets compare their contents.
[1, 2] == [1, 2]        # True — different objects, equal values
Float comparison with tolerance
Never == computed floats.
import math
math.isclose(0.1 + 0.2, 0.3)   # True

Examples

1. Value equality
[1, 2] == [1, 2]
Returns
True
2. Cross-type numerics
1 == 1.0
Returns
True
3. Unrelated types
1 == "1"
Returns
False
4. The float trap
0.1 + 0.2 == 0.3
Returns
False

Pitfalls

1. Computed floats rarely compare equal
Representation error accumulates.
False
0.1 + 0.2 == 0.3
False
Fix
math.isclose(0.1 + 0.2, 0.3)
True
2. == vs is
Equal values need not be the same object — and vice-versa matters for None.
Style error
if x == None:
works but wrong idiom (and __eq__ can lie)
Fix
if x is None:
identity check — the convention
3. bool is an int
True == 1 and False == 0 — visible in counting and dict keys.
Surprising
{True: "a", 1: "b"}
{True: 'b'} — same key!
Be aware
type-check when bools and ints mix
explicit handling

When to use

Use it
  • Any value comparison
  • Container content comparison
Reach for something else
  • None checks → is None
  • Computed floats → math.isclose
  • Same-object checks → is

Notes

Complexity
O(1) scalars; O(n) containers
Return
bool (custom __eq__ may return anything)
CPython impl
Objects/object.c :: PyObject_RichCompare (Py_EQ)
Memory
No allocation
Thread-safe
Yes for immutable operands

FAQ

Implement __eq__ (and keep __hash__ consistent if instances go into sets/dicts).

def __eq__(self, other):
    return isinstance(other, Point) and self.xy == other.xy

History

1.0
Core operator from the beginning.