+ 7
id == id
a,b = [1,2] [1,2] if id(a) == id(b): print("y") else: print("n") Solution: n a is assigned [1,2] b is assigned [1,2] values of a and b renamed to id(a) and id(b): So confused on why they are not ==. Thanks:).
7 Answers
+ 9
Because they are two seperate objects of list class in the memory and computer have given them different names or 'id' to recognize them.
Even though their 'values' are same but their 'identity' or simply 'id' is not equal...... š
you can print the ids of them using the print function and can see they aren't equal......
ex: - print(id(a), id(b))
+ 7
If you are going to do variable initialisation this way:
a = [1,2]
b = a
a and be will have the same content and also the same object id. So there is only 1 object but 2 vars that pointed to the object.
+ 7
Ids are a bit so similar to memory addresses in languages like C.
+ 5
Although a and b have same value but the two are different objects and hence will have different ids.
+ 4
[1,2] are the values of a and b, NOT the values of their id. Like Shashi Ranjan said, they're different objects, so they possess different id's, regardless of their values.
+ 1
Use missed comma in the initialization.
+ 1
Thanks everyone! I'll review Sololearn to practice. In the meantime, I'm still a bit confused. Can anyone provide any other examples, etc. so I could practice? Thanks.