Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Beverly Lee
Beverly Lee
1,084 Points

I don't understand when to use = and ==, I keep mixing them up

Like in the title, when should i use x = y as opposed to x == y?

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

As P G points out the single equals is the assignment operator, while the double equals is the equality operator. You can sort of think of a variable like a box where we're going to store a value. When we use the single equals sign we're saying what's going to go in that box:

x = 5

In this case we have a "box"(variable) named x and we're putting the value 5 inside of it. We're not asking it... we're telling it. But when we use the double equals it's a comparison operator. And this works more like a question:

if (x == 10)

Here, we're asking is the value currently in the box labeled x 10? No, it's not... it's 5. Hope this helps clear up the equals signs! :smiley:

Jason Anders
Jason Anders
Treehouse Moderator 145,858 Points

I am curious as to why this response was down-voted?? It clearly answers the question being asked and details an explanation with examples!

Treehouse Moderator

:dizzy:

Beverly Lee
Beverly Lee
1,084 Points

Thank you, very helpful!

hector villasano
hector villasano
12,937 Points

This = sign means assignment

when your assinging variable use "="

This == sign means equality

when your checking for equality use "==" for example checking if 7 == 7