Python one thing I dont understand | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Python one thing I dont understand

Try this in Python IDLE 2.0 == 2 ?? Output is True but id of 2.0 and 2 is diff 2.0 is 2 is False int and float are diff data types but still how 2.0 is equal to 2 why?/ Plz explain

14th Aug 2022, 6:22 AM
Amrit Sundarka
Amrit Sundarka - avatar
6 Answers
14th Aug 2022, 9:17 AM
Oma Falk
Oma Falk - avatar
+ 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
14th Aug 2022, 12:37 PM
Djeddi Mohamed Islem
Djeddi Mohamed Islem - avatar
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.
15th Aug 2022, 1:26 PM
Sreeju
Sreeju - avatar
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!
16th Aug 2022, 2:57 AM
Mauricio Martins
Mauricio Martins - avatar
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))
16th Aug 2022, 6:09 AM
Ali Muhammad