Python one thing I dont understand | Sololearn: Learn to code for FREE!

+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

8/14/2022 6:22:54 AM

Amrit Sundarka

6 Answers

New Answer

+3

https://code.sololearn.com/c6U9k19c03oV/?ref=app

+4

https://www.geeksforgeeks.org/difference-between-and-is-operator-in-python/

+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))