when I give two float numbers, it shows false, why? it should be true. why it shows false?
Asked
Active
Viewed 32 times
-1
-
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
-
1Does 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
-
1the 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 Answers
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