Thursday , September 19 2024

Take a look at the snippet and choose the true statement: nums = [1, 2, 3] vals = nums del vals[:]

Questions BankCategory: Python Essentials 1Take a look at the snippet and choose the true statement: nums = [1, 2, 3] vals = nums del vals[:]
Take a look at the snippet and choose the true statement:

nums = [1, 2, 3]
vals = nums
del vals[:]
  • the snippet will cause a runtime error
  • vals is longer than nums
  • nums and vals have the same length
  • nums is longer than vals

Explanation: Assigning nums to vals creates a situation in which the same list (i.e. [1, 2, 3]) has two different names. Giving a new name to an existing list, in our case vals to nums, is called aliasing. And since nums and vals are two different names that refer to the same object, they are also the same length.

The del instruction empties the list pointed to by nums and vals, which means the list has a zero length, which in turn means that nums and vals are the same length.

More Questions: Python Essentials 1 (PE1) Course Final Test