Q .What is the output of following code fragments?
(i) def increment(n): n. append([4]) return n L = [1, 2, 3] M = increment(L) print(L, M) (ii) def increment(n): n. append([49]) return n[0], n[1], n[2], n[3] L = [23, 35, 47] mi, m2, m3, m4 = increment (L) print(L) print(mi, m2, m3, m4) print(L[3] == m4)
Answer =
(i)
Output:-
[1, 2, 3, [4]] [1, 2, 3, [4]]
(ii)
Output: -
[23, 35, 47, [49]]
23 35 47 [49]
True
please explain for i)
ReplyDeleteThe increment function takes a list n as input.
DeleteInside the function, the .append([4]) method adds a new element, a list containing the value 4, to the end of the list n. Since n refers to the same list object as the one originally assigned to L, this modification is reflected in L as well.
The function returns the modified list, which is still L.
The line print(L, M) prints the values of L and M. Because M is assigned the result of increment(L), which ultimately points to the same modified list object, both L and M will contain [1, 2, 3, [4]].
Post a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )