+ 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

13th Feb 2022, 8:57 AM
IDIOT KID
IDIOT KID - avatar
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.
13th Feb 2022, 9:32 AM
Jayakrishna 🇼🇳
13th Feb 2022, 10:01 AM
Per Bratthammar
Per Bratthammar - avatar
+ 1
Jayakrishna🇼🇳 Per Bratthammar THANKS BOTH OF YOU, FOR EXPLANATION
13th Feb 2022, 10:23 AM
IDIOT KID
IDIOT KID - avatar