
Python Learning Notes
What Is the Difference Between = and == in Python?
A beginner-friendly Python note on assignment, equality comparison, and writing correct if conditions.
Python 学习笔记
= 和 == 到底差在哪?
一篇适合初学者的 Python 笔记,讲清楚赋值、相等比较,以及如何写对 if 条件判断。
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?
一个等号和两个等号,是很多 Python 新手最容易混的地方。尤其是写 if 条件判断时,少写或多写一个等号,程序的意思就完全不一样。
这一篇只讲一个核心区别:= 是赋值,== 是比较。你可以先记住一句话:一个等号做动作,两个等号问问题。
常见误解
新手看到这两行代码时,经常觉得它们差不多:
x = 5
x == 5
但它们的含义完全不同。
第一行:
x = 5
意思是把名字 x 指向数字对象 5。这是赋值,也就是 assignment。
第二行:
x == 5
意思是问:x 现在的值是不是等于 5?这是比较,也就是 comparison。比较的结果会是 True 或 False。
核心概念
= 是赋值。它告诉 Python:从现在开始,这个名字代表这个值。
x = 5
print(x)
5
这里 x = 5 不是在提问,而是在做一个动作:把 x 这个名字和 5 建立关系。
== 是比较。它问 Python:左边和右边是否相等?
x = 5
print(x == 5)
print(x == 7)
True
False
x == 5 的结果是 True,因为 x 当前指向的值是 5。x == 7 的结果是 False,因为它们不相等。
在条件判断里怎么用?
最常见的场景是 if:
x = 5
if x == 5:
print("yes")
yes
这里必须用 ==,因为 if 后面需要的是一个判断结果:这个条件是 True 还是 False?
如果你想保存一个值,用 =:
score = 90
如果你想判断是否相等,用 ==:
score == 90
对比一下
| 写法 | 作用 | 可以怎么理解 |
|---|---|---|
x = 5 |
赋值 | 让 x 指向 5 |
x == 5 |
比较 | 问 x 是否等于 5 |
if x == 5: |
条件判断 | 如果比较结果为 True,就执行代码块 |
为什么这个概念很重要?
在 Python 基础题和 CCC 输入判断题里,if 很常见。只要题目里出现“如果等于”“是否等于”“判断是不是”,你通常需要的是 ==,不是 =。
比如:
answer = input()
if answer == "yes":
print("continue")
这里第一行 answer = input() 是赋值:把输入结果保存到 answer。第三行 answer == "yes" 是比较:判断输入内容是不是 "yes"。
小练习
age = 12是赋值还是比较?age == 12的结果可能是什么类型?- 挑战题:写一段代码,保存
score = 100,然后判断score是否等于100。
一句话记住
一个等号 = 做动作:赋值;两个等号 == 问问题:是否相等。
下次写 if 条件判断时,先停一秒问自己:我是在保存一个值,还是在判断是否相等?
Questions or feedback?
Have a question about this article or want to suggest an improvement? Send us a private message.
有问题或建议?
如果你对本文有疑问,或希望提出改进建议,请给我们发送私密留言。
Related Learning
Continue exploring related learning paths.
These related pages help students and parents move from interest to the right next step.