Yes. In Python, == is the equality operator: it compares two values and returns True if they are equal and False otherwise. A single = is different, because it assigns a value to a name. Python does not convert types silently for ==, so 5 == "5" is False.
The operator works by calling the __eq__ method of the left operand, which means each type, and your own classes, decides what "equal" means.
== operator works
== versus = versus is
== in Python== operator worksa == b asks whether the values of a and b are equal. It does not ask whether they are the same object in memory.
The result is always a boolean for the built-in types. Chained comparisons are also allowed and read naturally:
Numbers of different types compare by mathematical value, and bool is a subclass of int:
This is not type coercion in the JavaScript sense. Python simply defines numeric equality across its number types, and there is no rule that turns a string into a number.
Lists, tuples, dictionaries and sets are equal when their contents are equal:
== versus = versus isThree symbols cause most beginner mistakes.
| Symbol | Meaning | Example |
|---|---|---|
= | Assignment | x = 5 |
== | Value equality | x == 5 |
is | Identity, same object | x is None |
:= | Assignment expression (walrus), Python 3.8+ | if (n := len(items)) > 3: |
Using = where you meant == inside an if is a syntax error in Python, which prevents the classic C bug of assigning by accident.
isUse is for singletons, most of all None, True and False. Do not use it to compare numbers or strings, because whether two equal values share one object is an implementation detail.
By default == on custom objects falls back to identity. Implement __eq__ to compare by value, or use a dataclass, which generates it for you.
If you write __eq__ by hand, return NotImplemented for types you do not know how to compare, so Python can try the other operand. If you define __eq__ on a class that should be hashable, also define __hash__ consistently, because setting __eq__ alone makes instances unhashable.
Floating-point numbers are stored in binary, so exact equality often fails:
Use math.isclose for tolerance-based comparison, or decimal.Decimal for money:
float("nan") == float("nan") is also False, so use math.isnan to test for NaN.
== in PythonMost bugs around equality come from a few repeated patterns. Knowing them saves debugging time.
input() always returns a string, so input("Age: ") == 18 is never true. Convert first with int(...) and handle ValueError.is for strings or integers. It may appear to work for small values because of interning, then fail elsewhere. Use ==.if x == True. Prefer if x: for truthiness, or if x is True when you need the exact boolean.== between a list and a tuple is False, even when the elements match.Also remember that == can be overridden. Libraries such as NumPy return an array of booleans from == instead of a single True or False, so use np.array_equal or .all() when you need one answer.
== and is in Python?== compares values by calling __eq__. is checks whether both names refer to the very same object. Use == for values and is only for None and other singletons.
== convert types like JavaScript?No. Python does not turn "5" into 5 for you. Different types are unequal unless their classes define equality between them, as numbers do.
!= do?It is the opposite of ==: it returns True when the values are not equal. In Python 3 it derives from __eq__ unless you override __ne__.
0.1 + 0.2 == 0.3 return False?Because of binary floating-point rounding. Compare with math.isclose or use Decimal.
== tests value equality in Python and returns a boolean, = assigns, and is tests identity. Python does not coerce strings to numbers, numeric types compare by value, and containers compare element by element. Define __eq__ or use a dataclass for your own types, and use math.isclose for floats.