+ 1
Help me with this code
Here, at line 12 output = [1,2,1,2,1,2,1,2] 13 output = [1,2,1,2,1,2,1,2] 16 output = [1,2,1,2,1,2,1,2] 17 output = [1,2] But the input given for line 13 and 17 are same then why output are different https://code.sololearn.com/c4g34kai4kLn/?ref=app
3 Answers
+ 1
No not same input.
[1, 2] is a list.
(1, 2) is a tuple.
[ ] lists are mutable in nature, you can change its data, after once you created it. While tuple is immutable data structure, once you create then you can't change its values...
that's why line: 17, v = (1, 2) is not modified its original v(outside function) , while line 13: list v=[1,2] are modified its original.
in function, you get a new copy for immutable types but for mutable types original variable gets passed. you can its addresses by print(id(v).
Tuple, strings, numbers , frozen sets are immutable types.
Lists, set, dictionary are mutable data structure types.
+ 1
Hi IDIOT KID
You can look at this:
https://code.sololearn.com/c6HP42TW4Q5O/?ref=app
+ 1
Jayakrishnađźđł Per Bratthammar THANKS BOTH OF YOU, FOR EXPLANATION