Is it preferred to do:
if x is y:
return True
or
if x == y
return True
Same thing for \"is not\"<
== and != are object value comparison operatorsis and is not are object identity comparison operatorsas others have already said, is (and is not) are only when you actually care that a pair of variables are referring to exactly the same object. in most cases, you really don't care at all, so you would use == and !=.
however, what you may start to notice, if you look at a lot of Python code, is that is (and is not) are more likely to be used when comparing against True, False, and None. the main reason for this is that those objects are singletons, meaning there is exactly one instance of each of those values. why does that matter? well, this leads to another reason... speed.
with == and !=, the interpreter has to pull up both referred objects in order to make a comparison (of whether they're the same or not), while is and is not simply just check the values of the objects they're referring to. with this said, you can see that the latter pair will perform faster because you don't have to fetch the objects themselves in order to make the comparison. here's a speed test from a couple of years back where we concluded that for one-offs, it's not a big deal, but if it's called a gazillion times in a tight loop, well, it will start to add up.
http://mail.python.org/pipermail/tutor/2008-June/062708.html
bottom line is that you can use object identity comparisons for checking against True, False, and None, and everything else should use straight-up equality operators. we won't get into interned integers nor bound instance methods, or anything like that here. :-)