6 odpowiedzi
+ 4
+ 4
In js there's the "===" that checks the equality in the value and in the data type but if you try 2.0==2 in js it'll still be true
0
As for why 2.0 == 2, it's because 2.0 and 2 represent the same number. Python doesn't require that two objects have the same type for them to be considered equal.
0
It`s basic...
The == operator compares the value (or equality) of two objects; See at this point python is comparing if 2.0 is numerically equals to 2;
But the Python *is* operator checks whether two variables point to the same object in memory. So 2.0 has its own location and proper type (float), and 2 will be in another memory location (and proper type: int). Thus they don`t share the same object in memory!
Have fun!
0
a = 2
b = 2.0
c = 2
#just comparing the result that equal or not.
print(a==b)
# here you can see the type of a and b is different.
print(type(a))
print(type(b))