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)

Python

Python Functions

4 Likes

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.

Answered By

1 Like


Related Questions