-1

when I give two float numbers, it shows false, why? it should be true. why it shows false?

shaik moeed
  • 5,300
  • 1
  • 18
  • 54
  • You want to check `a == b`? Read more about `is` vs `==` [here](https://stackoverflow.com/a/133024/8353711) – shaik moeed Oct 08 '22 at 08:22
  • 1
    Does this answer your question? ["is" operator behaves unexpectedly with integers](https://stackoverflow.com/questions/306313/is-operator-behaves-unexpectedly-with-integers) – Ahmed AEK Oct 08 '22 at 08:23
  • 1
    the only 2 situations where you should use `is` operator is when 1. you are comparing if two objects share the same memory location 2. comparing things with `None` explicitly to avoid type conversion. – Ahmed AEK Oct 08 '22 at 08:26

2 Answers2

3

The “is keyword” is used to test whether two variables belong to the same object, i.e. if they are referring to the same memory location.

So a is b is False and a == b is True.

This might help.

kgkmeekg
  • 524
  • 2
  • 8
  • 17
3

It shows false because both variables are not sharing the same memory location. For more, check here.

How to check this?

a = 8.5
b = 8.5
print(id(a), id(b)) # 140035857364400 140035857418800
print(a is b) # False
print(a == b) # True
shaik moeed
  • 5,300
  • 1
  • 18
  • 54