+ 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.

1st May 2019, 5:09 PM
KRA
KRA - avatar
5 Antworten
+ 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
1st May 2019, 5:18 PM
Dlite
Dlite - avatar
+ 3
You can review questions before they get accepted
2nd May 2019, 3:44 PM
Radu Baloi
Radu Baloi - avatar
+ 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
14th May 2019, 10:57 AM
Supun
Supun - avatar
- 1
...
2nd May 2019, 9:04 AM
Grace CLARKE
Grace CLARKE - avatar
- 1
error
2nd May 2019, 11:23 AM
Rohit Raj
Rohit Raj - avatar