Skip to main content

How to Check if Two Values Are Equal in Python

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

Most decisions in a program start with a question: is this value the one I'm looking for? == answers it with True or False. This guide compares numbers, strings and lists, shows the one result that surprises everyone, and uses == in an if, with real output from Python 3.15.2.

Syntax​

Syntax
<left> == <right>
PartWhat it means
<left>, <right>Any two values or expressions
==Two equals signs. Gives True when the values are equal, False otherwise

Python's language reference: "The operators <, >, ==, >=, <=, and != compare the values of two objects. The objects do not need to have the same type."

Prerequisites​

  • uv and Python. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

compare.py
print(3 == 3)
print("tent" == "Tent")
print([1, 2] == [1, 2])
print(0.1 + 0.2 == 0.3)

answer = "quit"
if answer == "quit":
print("Stopping")

The compare.py script in a macOS-style code window: four printed comparisons with ==, then an if statement that prints Stopping when answer equals quit.

Step 2: Run It​

Terminal
uv run python compare.py
Output
True
False
True
False
Stopping

The output in a terminal: True, False, True, False, then Stopping.

Line by line:

  • 3 == 3 is True.
  • "tent" == "Tent" is False: string comparison counts capital letters.
  • [1, 2] == [1, 2] is True: two lists are equal when their items are equal, in order.
  • 0.1 + 0.2 == 0.3 is False: decimal fractions are stored in binary, and the sum lands a tiny bit off 0.3. Compare such numbers with math.isclose(0.1 + 0.2, 0.3), which returns True.

In the if, the comparison was True, so Stopping printed.

Common Mistakes​

MistakeWhat happensDo this instead
Writing one = in a condition, as in if x = 1:SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?Use == to compare; = assigns
Comparing user input without normalizing it"Quit" isn't equal to "quit"Compare answer.strip().lower()
Comparing calculated decimals with ==0.1 + 0.2 == 0.3 is FalseUse math.isclose()

Troubleshooting​

SymptomLikely causeFix
A comparison you expected to be True is FalseDifferent types, such as "3" == 3, or hidden spacesConvert both sides to the same type and strip spaces

FAQ​

What's the difference between == and is?​

== compares values; is checks whether both sides are the very same object. [1, 2] == [1, 2] is True, but [1, 2] is [1, 2] is False, because they're two separate lists.

How do I check that two values are different?​

Use !=, the opposite of ==.

Conclusion​

== returned True for equal numbers and lists, False for "tent" and "Tent" and for 0.1 + 0.2 against 0.3, and drove the if that printed Stopping.

Additional Resources​