954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Python "is" statement

print "Hello, World!"
;)
Hi, everyone, Venku (a.k.a Python Newb) here.
I just kind of randomly opened up Python today (I do that a lot), and I decided I would finally ask this:

What does the "is" statement do in Python 2.7.1 (if the ver# matters)?

I checked out the Python command line (used "help()" "modules" "is"), but it only gave a comparison to C and an unintelligible description of what it does. I have an extensive vocabulary, and I am great with grammar and spelling, but the terminology, like "collection" and "membership" confused me.

It appears to be used in boolean operations (equal, not equal, etc.).
I wouldn't call myself a programmer just yet, but I'm very interested in Python and think it could be applied effectively in many environments, as it's so flexible.

Basically, I'm asking for a n00b explanation of "is."
:P

Venku Tur'Mukan
Newbie Poster
22 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

The "is" statement should only be used when you want to check if
something is exactly and identical to None like "a is None" or "b is not None".
For everything else you should use == or !=.
There are some other use cases for "is", too.
But in general you shouldn't use "is".

snippsat
Practically a Posting Shark
808 posts since Aug 2008
Reputation Points: 353
Solved Threads: 294
 

You use 'is' to check the identity of objects ...

# 'is' checks object identities
# the id is a memory location
# in this case Python stores the 7 in the same location

a = 7
b = 7
print(id(a))   # 30626568
print(id(b))   # 30626568
print(a is b)  # True


Do not use 'is' instead of '==' (there can be some problems).

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

Thanks, you were both helpful!
I knew to use "==" but when I noticed the "is" statement I was confused.

Thanks again!

Venku Tur'Mukan
Newbie Poster
22 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: