New course! Every coder should learn Generative AI!
Try a free lesson+ 4
yash please remove all your spam comments and only answer with relevant comments to the current question.
Thanks and happy coding.
https://www.sololearn.com/discuss/1316935/?ref=app
https://code.sololearn.com/Wv5gTHy1N6Ji/?ref=app
+ 3
Since both types are lists, it will give the answer as true
+ 2
you are passing the same list to foo function and just changing its values to list values and returning the id of that list (basically the id of list q) instead of creating a new list and returning it
As an example
def foo(x):
x[0]=["eggs"]
x[1]=["spam"]
return id(list(x))
q=["spam","eggs"]
print(id(q)==foo(q))
this will return false
+ 2
it is giving true as both id(q) and foo(q) are same lists
+ 1
the function gets the id of x.
q AND x are both lists so when you compare types it equals True.
+ 1
Here's simple example:
https://code.sololearn.com/c6WnZgE4zT64/?ref=app
+ 1
The code mutated the list, so the identity is same.
The id() function returns identity (unique integer) of an object. And all objects in Python can be either mutable or immutable. In simple words, mutable means 'able to be changed’ and immutable means ‘constant’.
Try following example:
https://code.sololearn.com/cO74rFh6RIci/#py
The identity of Lists are still the same after modified, but the Numbers are different.
P.S.
Mutable objects:
list, dict, set
Immutable objects:
int, float, complex, string, tuple