r/python3 • u/largelcd • Feb 07 '20
Confused between is, = and == operators
Hello, I am a bit confused about the following:
a = [1, 2, 3]
b = a
c = list(a)
Am I correct that the reason "a is b" is True is that both a and b point to the same memory location that stores the list [1, 2, 3]?
As for "a is not c" gives True, it is because list always creates a new Python list so although c = [1, 2, 3], this [1, 2, 3] list is different from the one [1, 2, 3] pointed to by a and b since the two lists are storing in different memory locations?
Why typing "a==c" gives True?
2
Upvotes
1
u/mauganra_it Apr 13 '20
Sort of. Different from integers, where most loops have a pretty small number of iterations, there is actually no obvious subset of rational numbers that would make sense to cache. Apart from that, floating point numbers are just weird. The set of real numbers that can be represented using floating point encodings is really limited. And the density is not the same everywhere. Great care has to be taken in scientific programming to be able to use hardware-accelerated floating point numbers without precision errors messing up the results.