ProgrammingHardProduct Company SDE · Python internals
What does the following Python 3 code print? a = [1, 2, 3] b = a b.append(4) print(a)
- A. [4, 1, 2, 3]
- B. Error
- C. [1, 2, 3, 4]
- D. [1, 2, 3]
Correct answer: C. [1, 2, 3, 4]
b = a does not copy the list; both names reference the same object, so appending through b also changes what a sees.
More Python Programming questions
- What does the following Python 3 code print? a = [1, 2, 3] b = a[:] b.append(4) print(a, b)
- What does the following Python 3 code print? def f(a, b, args): return a, b, args print(f(1, 2, 3, 4, 5))
- What does the following Python 3 code print? def f(kwargs): return sorted(kwargs.items()) print(f(b=2, a=1, c=3))
- What does the following Python 3 code print? def f(a, , b): return a + b try: print(f(1, 2)) except TypeError: print("err")
- What does the following Python 3 code print? l = [1, 2, 3] print(l, sep="-")
- What does the following Python 3 code print? print([1, 2, 3], [4, 5], sep=",")