-1
a=999999
b=999999
if a is b:
    print("yes")
else:
    print("no")

The answer to the following code is no.

Why python is not able to compare a and b inspite of the fact that both the values are equal to 999999

Habib Ansari
  • 3
  • 1
  • 6

3 Answers3

1

The correct way to compare two values for equality is:

a == b

When you use is, you're comparing object identity, not equality.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • 1
    then why does `is` works for lower values ? – Habib Ansari Sep 29 '17 at 18:27
  • 2
    @HabibAnsari because small integers are pooled, and in that case the identity check succeeds. See this article: https://davejingtian.org/2014/12/11/python-internals-integer-object-pool-pyintobject/ – Óscar López Sep 29 '17 at 18:28
1

Try this...!

a=999999
b=999999
if a == b:
    print("yes")
else:
    print("no")
Gowtham Balusamy
  • 728
  • 10
  • 22
1

You are checking if the two variables are the same obect with is. You are most likely looking for ==

a == b

MattR
  • 4,887
  • 9
  • 40
  • 67
histo
  • 11
  • 1