0

I have following simple code. Just confused why the second evaluation is True. Could anyone explain to me? So what is exactly the difference between "is" and "=="?

a = float("inf")
print a != float("inf")
print a is not float("inf") # why is this True?

b = 1
print b != 1
print b is not 1

c = int(1)
print c != int(1)
print c != int(1)

Output:

Python 2.7.10 (default, Jul 14 2015, 19:46:27)
[GCC 4.8.2] on linux

False
True
False
False
False
False
SixSigma
  • 2,808
  • 2
  • 18
  • 21
  • 1
    @Prajwal: You're thinking of some other language's use of the keyword. `is` is an object identity test in Python. – user2357112 Jan 11 '17 at 05:44
  • One more question. Then wouldn't it be preferred using "==" if just for checking arithmetic equality? I feel using "is" sounds more pythonic though. – SixSigma Jan 11 '17 at 05:52
  • 1
    Don't write code based on what it sounds like. That'd be stupid. Write code based on what it actually means. You want `==`. – user2357112 Jan 11 '17 at 05:57

0 Answers0