= 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
| Operator | Name | Function | Example |
|---|---|---|---|
= |
Assignment | Gives a value to a variable | x = 10 → x becomes 10 |
== |
Comparison | Checks if two values are equal | 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
Think of it 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
- Using
=insideiforwhileconditions — 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 —
x = 10 - == is for comparing values —
x == 10 - != means "not equal" —
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.
What does if __name__ == '__main__' mean? · Coding roadmap with no experience · Learn coding without a CS degree?
Hope this cleared your doubt! More coming soon — keep learning with Kiddo 🎉