4Geeks chosen to deliver AI education in the Bahamas alongside Harvard, Oxford, and Columbia.See more

What does a double equal sign mean in Python?

By 4Geeks

Compare values without surprises: when == returns True, how it differs from = and is, and what to use for floats.

6 min readLast updated
Python == operator icon

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

OperatorWhat it doesExampleResult
=Assigns a valuex = 5x now holds 5
==Compares valuesx == 5True
!=Not equalx != 3True
isSame object in memoryx is NoneFalse
is notNot the same objectx is not NoneTrue
:=Assigns inside an expressionif (n := len(items)) > 3:Assigns and compares

How == compares different types

Python compares the values, and some types count as equal to each other:

python
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 list

Data 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:

python
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 a

Use 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:

python
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:

python
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))  # True

Use 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().

Career · AI Engineering

You can compare values, now build something with it

From zero to junior AI Engineer: Python, AI agents and RAG with mentors and real projects.

Use == with AI

An AI assistant such as ChatGPT, Claude or GitHub Copilot can explain a comparison that does not behave as you expect. Give it the two values, their types and the result you got. These three prompts are ready to copy.

1. Explain a comparison that returns False

In Python, this comparison returns [False] but I expected [True]:
[paste the line, e.g. if answer == 5:]
The values are: [paste them, and the output of print(type(answer))]
Explain why and give me the fix.

2. Choose between ==, is and isclose()

In my Python code I compare [prices / user names / API responses / None values].
Tell me whether I should use ==, is or math.isclose() in each case and why.
This is my code: [paste it]

3. Make my class comparable

I have this Python class: [paste it]
Two objects with the same data return False with ==.
Show me how to add __eq__, or how to turn it into a dataclass, and write tests for it.

Check the answer by printing both values and their types with print(repr(a), type(a)).

Compare strings without case or extra spaces

== compares strings character by character, so "Yes", "yes" and "yes " are three different values. Normalize them first:

python
answer = " Yes "
print(answer == "yes")                  # False
print(answer.strip().lower() == "yes")  # True

For text in other languages, casefold() is more complete than lower(): "Straße".casefold() == "STRASSE".casefold() is True.

Compare several values at once

Python lets you chain comparisons, and in replaces several == joined with or:

python
a = b = c = 7
print(a == b == c)  # True, the same as a == b and b == c
 
color = "red"
print(color == "red" or color == "blue")  # True
print(color in ("red", "blue"))           # True, shorter

== with your own classes

By default, two objects of your own class are only equal if they are the same object. A dataclass compares the fields for you:

python
from dataclasses import dataclass
 
@dataclass
class Point:
    x: int
    y: int
 
print(Point(1, 2) == Point(1, 2))  # True

Without @dataclass, the same comparison returns False unless you write an __eq__ method.

Learn with a mentor

Write conditions you can trust, then build AI with Python

In the 4Geeks AI Engineering career program you start with no experience and finish with AI agents and RAG systems in your portfolio.

Common errors with == in Python

What you seeWhy it happensFix
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?You wrote = inside an ifUse == to compare
input() compared with a number is always Falseinput() returns a stringConvert it: int(answer) == 5
0.1 + 0.2 == 0.3 returns FalseFloats have tiny rounding errorsUse math.isclose()
SyntaxWarning: "is" with 'int' literalYou used is to compare a numberUse ==
Two objects with the same data are not ==Your class has no __eq__Add __eq__ or use @dataclass
"Yes" == "yes" returns FalseComparisons are case-sensitiveUse .lower() or .casefold() on both sides
Free download

==, = and is explained in a 2-page cheat sheet

What == returns with numbers, strings, lists, dicts and floats, how it differs from = and is, recipes, the most common errors and prompts for ChatGPT, Claude or Copilot. Free, in PDF.

Python == operator cheat sheet

2 pages · PDF · 4Geeks

  • 9 comparisons with their results and why
  • When to use ==, is or math.isclose()
  • The 5 most common errors and prompts to fix them with AI

Get it free

Your PDF right away

Frequently asked questions about == in Python

Want to go from Python basics to building AI agents, with real projects and mentors?

See the AI Engineering program