Computer Science

Are these values equal? Why/why not?

  1. 20 and 20.0
  2. 20 and int(20)
  3. str(20) and str(20.0)
  4. 'a' and "a"

Python Data Handling

47 Likes

Answer

  1. The type of 20 is int whereas the type of 20.0 is float so they are two different objects. Both have the same value of 20. So, as values are same equality (==) operator return True but as objects are different is operator returns False.
  2. The value and type of both 20 and int(20) are the same and both point to the same object so both equality (==) and is operator returns True.
  3. For str(20) and str(20.0), both equality (==) and is operator returns False as their values are different and they point to two different objects.
  4. For 'a' and "a", both equality (==) and is operator returns True as their values are same and they point to the same object.

Answered By

19 Likes


Related Questions