+ 8
Who can review the challenge questions?
a=[1,[2,3]] b=[1,2,3] c=a[:] d=b[:] print(c is a,d is b) Answer: False,False Why don't we get True,True as the answer?Pease explain this.
5 Respostas
+ 5
The reason lies in the fact that you didn't reassign the list "a" to "c".
Rather, you made a separate copy of the list in "a", and assigned that copy to list "c"
By doing c = a[:]
You are assigning a copy of a to c.
If you remove the "[:]", then the outcome is True, True
+ 3
You can review questions before they get accepted
+ 3
it is called the object identity. object identity will not be same even if values of two variables are same. you can check the object identity of two varibles using id keyword.
is keyword check whether two variables are the same object or not. you will understand this in object orianteted programing in python
- 1
...
- 1
error