Computer Science
What is the output of following code fragments ?
def increment(n):
n.append([49])
return n[0], n[1], n[2], n[3]
L = [23, 35, 47]
m1, m2, m3, m4 = increment(L)
print(L)
print(m1, m2, m3, m4)
print(L[3] == m4)
Answer
[23, 35, 47, [49]]
23 35 47 [49]
True
Working
The function increment
appends [49] to list n
and returns its first four elements individually. When L = [23, 35, 47]
, calling increment(L)
modifies L to [23, 35, 47, [49]]. Variables m1, m2, m3, and m4
are assigned the same list [23, 35, 47, [49]], representing the original list L. Thus, printing L and m1, m2, m3, m4 yields [23, 35, 47, [49]]. The expression L[3] == m4 evaluates to True, indicating that the fourth element of L is the same as m4.
Related Questions
Write a program with non-void version of above function and then write flow of execution for both the programs.
What is the output of following code fragments ?
def increment(n): n.append([4]) return n L = [1, 2, 3] M = increment(L) print(L, M)
What will be the output of the following Python code ?
V = 25 def Fun(Ch): V = 50 print(V, end = Ch) V *= 2 print(V, end = Ch) print(V, end = "*") Fun("!") print(V)
- 25*50!100!25
- 50*100!100!100
- 25*50!100!100
- Error
Write a function that takes amount-in-dollars and dollar-to-rupee conversion price; it then returns the amount converted to rupees. Create the function in both void and non-void forms.