not-operator

What is more 'pythonic' for 'not'

∥☆過路亽.° 提交于 2019-11-26 17:55:14
I have seen it both ways, but which way is more Pythonic? a = [1, 2, 3] # version 1 if not 4 in a: print 'is the not more pythonic?' # version 2 if 4 not in a: print 'this haz more engrish' Which way would be considered better Python? The second option is more Pythonic for two reasons: It is one operator, translating to one bytecode operand. The other line is really not (4 in a) ; two operators. As it happens, Python optimizes the latter case and translates not (x in y) into x not in y anyway, but that is an implementation detail of the CPython compiler. It is close to how you'd use the same

Python not equal operator

我怕爱的太早我们不能终老 提交于 2019-11-26 11:38:19
问题 I come from a c style languages, so I am natural in using != as not equal, but when I came to Python, from the documentation I read, I learned that for this purpose the <> operator is used. Recently, I have seen a lot of code using != , so my question is if one of them is preferred over the other or is one of them deprecated. Also, I would like to know if there is any difference between them. 回答1: Python 2 supports both , in python 3 the <> operator has been removed. There is no difference