What Does == Mean in Python? Equality Operator Explained

== in Python is the equality operator. It compares two values and returns True if they are equal and False if they are not. In Python’s language reference, == is one of the value comparison operators, while is checks object identity instead.

If you are learning Python, this operator shows up everywhere: in if statements, loops, functions, input checks, tests, and everyday programming logic. It looks simple, but many beginners confuse it with = or is.

That confusion causes bugs fast. This guide explains what == means in Python, how it works with numbers, strings, lists, dictionaries, sets, custom objects, None, NaN, and more, plus the most common mistakes to avoid.


Quick Answer: What == Does

In Python, == asks: Are these two values equal?

Example:

5 == 5

Output:

True

Another example:

5 == 3

Output:

False

That is the core meaning of ==: it performs a value comparison and gives back a Boolean result, either True or False. Python lists == as the comparison operator for “equal.”


== vs = vs is

This is the most important section for beginners.

SymbolNameWhat it doesExample
=Assignment operatorAssigns a value to a variablex = 10
==Equality operatorCompares two valuesx == 10
isIdentity operatorChecks whether two names refer to the same objectx is y

Python’s docs separate value comparison from identity comparison. Objects in Python have an identity, a type, and a value. The is operator compares identity, while == compares value.

= assigns a value

x = 10

This stores 10 in the variable x.

== compares a value

x == 10

This checks whether the value in x is equal to 10.

is checks identity

a = [1, 2]
b = [1, 2]a == b
a is b

The first comparison is True because the list values match. The second is usually False because they are two different objects. Python’s data model says every object has its own identity, and is compares that identity.


How Python Decides Equality

Python compares the value of objects with ==, but the exact meaning of “equal” depends on the object type and its comparison methods.

Built-in and user-defined types can customize comparison behavior through rich comparison methods such as __eq__(). By default, equality for user-defined instances is based on identity unless the class defines __eq__.

Objects do not always need the same type

Python’s language reference says the objects being compared do not need to have the same type. For example, numbers of different built-in numeric types can compare mathematically. That is why this is true:

5 == 5.0

It returns True. Python compares the exact numeric values.

But most different types do not compare equal

Unless a type says otherwise, objects of different types usually do not compare equal. So this returns False:

"5" == 5

That is because one side is a string and the other is an integer. Python’s built-in types documentation notes that, unless stated otherwise, objects of different types never compare equal.


Examples of == With Common Python Data Types

Numbers: int, float, and bool

10 == 10        # True
5 == 5.0 # True
True == 1 # True
False == 0 # True

Python treats bool as a subtype of int, and numbers of different numeric types compare by value. That is why True == 1 and False == 0 are both true, even though using that style in real code can be confusing.

Strings

"python" == "python"   # True
"Python" == "python" # False

Strings compare by their Unicode code points, so comparison is exact and case-sensitive.

Bytes and bytearray

b"abc" == bytearray(b"abc")   # True

Python allows binary sequences such as bytes and bytearray to compare within and across their types.

Lists, tuples, and range

[1, 2, 3] == [1, 2, 3]     # True
(1, 2) == (1, 2) # True
[1, 2] == (1, 2) # False
range(3) == range(3) # True

Built-in sequences compare lexicographically, but equality across different sequence types results in inequality. So a list and a tuple with the same visible contents are still not equal.

Dictionaries

{"name": "Aamir"} == {"name": "Aamir"}   # True

Dictionaries compare equal when they have the same key-value pairs.

Sets and frozensets

{1, 2} == {2, 1}                 # True
set([1, 2]) == frozenset([2, 1]) # True

Python allows set and frozenset to compare across their types. Equality checks the members, not the order.


Real Examples of == in Python Code

Checking user input

language = input("Enter a language: ")if language == "Python":
print("You entered Python")

This is one of the most common beginner use cases. == checks whether the input value matches the expected text.

Using == in an if statement

score = 100if score == 100:
print("Perfect score")

Since == returns True or False, it works naturally inside if, elif, while, and other control flow statements. Python’s control-flow tutorial also reflects that equality is part of common branching logic.

Comparing two variables

x = 25
y = 25if x == y:
print("The values match")

The variable names do not matter. Python compares the values stored inside them.


Surprising Equality Results Beginners Should Know

ExpressionResultWhy
5 == 5.0TrueNumeric types compare by value
True == 1Truebool is a subtype of int
"5" == 5FalseDifferent types
[1, 2] == (1, 2)FalseList and tuple are different sequence types
float("nan") == float("nan")FalseNaN is not equal to itself
None == NoneTrueSame singleton value, though style prefers is

The NaN case is especially important. Python’s reference says not-a-number values like float('NaN') are not equal to themselves, following IEEE 754 behavior.


== vs is: When Each One Is Correct

Use == when you want to compare values.

name = "Sara"
name == "Sara"

Use is when you want to compare identity, especially for singleton objects like None.

result = Noneif result is None:
print("No result")

PEP 8 says comparisons to singletons like None should always be done with is or is not, never ==.

Do not use is for normal string, number, or list comparison

These are poor style:

name is "Ali"
age is 20
items is [1, 2]

These are better:

name == "Ali"
age == 20
items == [1, 2]

Python says the behavior of is cannot be customized and it always checks identity, not value.


Custom Classes and the __eq__ Method

For user-defined classes, == can use the __eq__() method. If you do not define it, non-identical instances normally compare as non-equal.

Here is a stronger example:

class User:
def __init__(self, email):
self.email = email def __eq__(self, other):
if not isinstance(other, User):
return NotImplemented
return self.email == other.email

Now this works as expected:

u1 = User("sam@example.com")
u2 = User("sam@example.com")print(u1 == u2) # True

Python’s data model allows rich comparison methods to customize equality behavior, and NotImplemented is one of Python’s singleton values.


Common Mistakes to Avoid

1. Using = instead of ==

Wrong:

if x = 5:
print("Hello")

Correct:

if x == 5:
print("Hello")

= assigns. == compares.

2. Using is when you mean ==

Wrong idea:

if username is "admin":
...

Better:

if username == "admin":
...

3. Forgetting case sensitivity

"Python" == "python"   # False

If needed, normalize first:

"Python".lower() == "python".lower()

4. Comparing values with different types by accident

input_age = "18"
input_age == 18 # False

Convert first:

int(input_age) == 18

5. Assuming == checks memory location

It does not. == checks equality of value, while is checks identity.


Does Python Have ===?

No. Python’s comparison operators include ==, !=, <, <=, >, >=, is, and is not. There is no === operator in standard Python syntax. In Python, == handles value equality and is handles identity.


Best Practices for Using == in Python

Use these simple rules:

  • Use == for normal value comparison.
  • Use is and is not for None.
  • Be careful when comparing strings that may differ in case.
  • Watch out for values that look the same but have different types.
  • When writing classes, define __eq__() if equality should depend on data instead of object identity.

Practical Takeaways

If you remember only a few things, remember these:

  • == means equal to
  • It compares values
  • It returns True or False
  • = assigns a value
  • is checks object identity
  • None should usually be checked with is None
  • Custom objects may need __eq__() to support meaningful equality

FAQ

What does == return in Python?

== returns a Boolean value: True if the two values are equal, and False if they are not.

What is the difference between = and == in Python?

= assigns a value to a variable, while == compares two values for equality.

What is the difference between == and is in Python?

== compares values. is compares identity, meaning whether two references point to the same object.

Does == compare type in Python?

Not directly. Python can compare some values across types, especially numeric types, but most objects of different types do not compare equal unless their type behavior says otherwise.

Why is 5 == 5.0 true in Python?

Because Python compares numbers of different numeric types by their mathematical value.

Why is True == 1 true in Python?

Because bool is a subtype of int in Python, so True behaves like 1 and False behaves like 0 in comparisons.

Why should I use is None instead of == None?

PEP 8 recommends comparing singletons like None with is or is not, not the equality operators.

Does Python have triple equals ===?

No. Python does not use ===. Use == for value equality and is for identity.


Conclusion

So, what does == mean in Python? It is the equality operator. It compares two values and tells you whether they are equal. Once you understand the difference between ==, =, and is, Python code becomes much easier to read, debug, and write with confidence.

If this article is part of a Python content cluster, the best next internal links would be pages about Python variables, comparison operators, if statements, booleans, None, strings, lists, and object-oriented programming.


Click Here To Read About: What Does Pretentious Mean? Definition, Examples, Synonyms

Leave a Comment