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
Liam Hayes
Full Stack JavaScript Techdegree Student 13,116 PointsI was wondering, what is "is not"? Is it similar to "!=" ?
How's going everybody? v(⌒o⌒)v
(If you're wondering where I ran into this problem, this is from the Learn Python Track, in the Python Collections course, from the video "Shopping List Take 3", at 6:53)
Let's say I have this code
position = input("In what position (index) do you want to insert your item into the list?")
if position is not None:
my_list.append(position)
else:
break
Do you notice the "is not"? How does that compare to "!="?
if I put instead
if position != None:
is that doing something different?
Thank you very much ^_^
1 Answer
Steven Parker
243,344 PointsOne is a negative identity test ("is not") and the other is a negative equality test ("!="). Identity means that both items are references to the same thing, where equality means that both items have the same value.
Now there's no difference when using "None", because it is always a reference to a specific internal object. But it can be quite different with other things.
To illustrate, let's simplify it a bit by using the positive versions of these comparisons ("is" and "=="):
a = 999
b = a
a is b True (because a and b both refer to the same thing internally) But...
a = 999
b = 999
a is b False (because a and b refer to different internal storage locations)
a == b True (because they do have the same value)
if you try this example with small values (256 and below) internal optimization will confuse the results.
Liam Hayes
Full Stack JavaScript Techdegree Student 13,116 PointsLiam Hayes
Full Stack JavaScript Techdegree Student 13,116 PointsThank you very much bro. I appreciate it.