Welcome to the FAQ Zone — Got questions? We got receipts
= vs == in Python: What’s the Difference?
This is one of the most common beginner confusions in Python! Don’t worry — by the end of this page, you’ll never mix these two up again
Key Difference (Comparison Table)
Operator | Name | Function | Example |
---|---|---|---|
= |
Assignment Operator | Gives a value to a variable | x = 10 → x becomes 10 |
== |
Comparison Operator | Checks if two values are equal | x == 10 → True if x is 10 |
Operator | Name |
---|---|
= | Assignment Operator |
== | Comparison Operator |
Operator | Function |
---|---|
= | Gives a value to a variable |
== | Checks if two values are equal |
Operator | Example |
---|---|
= | x = 10 → x becomes 10 |
== | x == 10 → True if x is 10 |
= (Assignment Operator)
The =
symbol is used to assign a value to a variable. You're basically saying, “Hey Python, store this under that name.”
x = 7
name = "Kiddo"
is_active = True
Quick Breakdown : It's like labeling a jar — the jar is the variable and the content is the value.
== (Equality Operator)
The ==
operator is for comparison. It checks if two values are equal and gives you a True
or False
result.
x = 7
print(x == 7) # True
print(x == 5) # False
Think of it like: “Are these two things *actually* the same?”
Common Mistakes (Don’t fall for these traps!)
- Using
=
insideif
orwhile
conditions — this throws a syntax error! - Using
==
when you meant to assign something.
Example: Let’s Make It Real
age = 18
if age == 18:
print("You're officially an adult!")
Why this works? Because we're comparing the variable age
with the number 18
. If they match, the message is printed!
Also Meet: !=
(Not Equal To)
While we’re at it, here’s a sibling operator! !=
checks if two values are not equal.
score = 45
if score != 100:
print("Not perfect yet!")
Quick Breakdown
- = is for assigning values (e.g.,
x = 10
). - == is for comparing values (e.g.,
x == 10
). - != means “not equal” (e.g.,
x != 10
).
When to Use What?
- Use
=
when you're setting or updating values. - Use
==
when checking conditions (like inside anif
). - Use
!=
when you want to exclude a match.
Hope this cleared your doubt!
Explore More FAQs Like This
- What Does if __name__ == '__main__' Mean in Python?
- What’s the difference between list and tuple?
- What does `None` mean in Python?
- How is Python interpreted line by line?
More coming soon — stay tuned and keep learning with Kiddo!