How to Check if Two Values Are Equal in Python
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
<left> == <right>
| Part | What 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
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")

Step 2: Run It
uv run python compare.py
True
False
True
False
Stopping

Line by line:
3 == 3isTrue."tent" == "Tent"isFalse: string comparison counts capital letters.[1, 2] == [1, 2]isTrue: two lists are equal when their items are equal, in order.0.1 + 0.2 == 0.3isFalse: decimal fractions are stored in binary, and the sum lands a tiny bit off0.3. Compare such numbers withmath.isclose(0.1 + 0.2, 0.3), which returnsTrue.
In the if, the comparison was True, so Stopping printed.
Common Mistakes
| Mistake | What happens | Do 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 False | Use math.isclose() |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
A comparison you expected to be True is False | Different types, such as "3" == 3, or hidden spaces | Convert 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
- Value comparisons — how
==compares each kind of value.
