4

Python version: Python 3.5.1 (v3.5.1:37a07cee5969, Dec 6 2015, 01:38:48) [MSC v.1900 32 bit (Intel)] on win32.

>>> 256 is (2**8)
True
>>> 512 is (2**9)
False

UPD

>>> print(id(256), id(2**8))
1933723392 1933723392
>>> print(id(512), id(2**9))
60976880 60976704
pank
  • 803
  • 1
  • 11
  • 16

1 Answers1

2

is checks to see if two variables are stored at the same memory location. The following states that the two numbers are stored at different locations in memory:

>>> 512 is (2**9)
False

Very likely, what you really wanted to know was if the numbers were equal. To do that, test for equality:

>>> 512 == (2**9)
True

Exceptional case: None

None does not have any sensible value. Consequently, checking if something is equal to None is generally not useful. To find out if some variable is None, use is:

>>> x = None
>>> x is None
True
John1024
  • 109,961
  • 14
  • 137
  • 171
  • `==` can be thought of as "value equality", that is, if two things look the same, `==` should return `True`. `is` can be thought of as `object identity`, that is, the two things actually are the same object. `object identity` normally translates as having the same memory address. – NZD Aug 20 '16 at 07:28