One equals sign and two equals signs are easy to mix up when you are new to Python. This is especially common inside if statements, where one missing equals sign can completely change the meaning of your code.
This note focuses on one core difference: = means assignment, and == means comparison. A simple way to remember it is: one equals sign does an action; two equals signs ask a question.
Common Mistake
Beginners often look at these two lines and think they are almost the same:
x = 5
x == 5
But they mean very different things.
The first line:
x = 5
means that the name x now refers to the number object 5. This is assignment.
The second line:
x == 5
asks: is the current value of x equal to 5? This is comparison. The result of a comparison is True or False.
Core Idea
= is assignment. It tells Python: from now on, this name refers to this value.
x = 5
print(x)
5
Here, x = 5 is not asking a question. It is doing an action: it connects the name x with the value 5.
== is comparison. It asks Python whether the left side and the right side are equal.
x = 5
print(x == 5)
print(x == 7)
True
False
x == 5 gives True because the current value of x is 5. x == 7 gives False because they are not equal.
How Do We Use This in if?
The most common place to use == is inside an if statement:
x = 5
if x == 5:
print("yes")
yes
Here we need ==, because if needs a condition that becomes True or False.
If you want to save a value, use =:
score = 90
If you want to check whether two values are equal, use ==:
score == 90
Comparison
| Code | Purpose | How to think about it |
|---|---|---|
x = 5 |
Assignment | Make x refer to 5 |
x == 5 |
Comparison | Ask whether x equals 5 |
if x == 5: |
Conditional check | Run the block if the comparison is True |
Why This Matters
In Python basics and CCC-style input problems, if statements show up everywhere. When the problem says “if it equals,” “whether it equals,” or “check if it is,” you usually need ==, not =.
For example:
answer = input()
if answer == "yes":
print("continue")
The first line, answer = input(), is assignment: it saves the input in answer. The third line, answer == "yes", is comparison: it checks whether the input is "yes".
Practice
- Is
age = 12assignment or comparison? - What type of result can
age == 12produce? - Challenge: write code that saves
score = 100, then checks whetherscoreequals100.
One-Sentence Takeaway
One equals sign = does an action: assignment. Two equals signs == ask a question: are these equal?
Next time you write an if statement, pause for one second and ask: am I saving a value, or checking equality?
Questions or feedback?
Have a question about this article or want to suggest an improvement? Send us an email.
