Thursday , September 19 2024

The second assignment: vals = [0, 1, 2] vals[0], vals[2] = vals[2], vals[0]

Questions BankCategory: Python Essentials 1The second assignment: vals = [0, 1, 2] vals[0], vals[2] = vals[2], vals[0]
The second assignment:

vals = [0, 1, 2]
vals[0], vals[2] = vals[2], vals[0]
  • doesn’t change the list
  • shortens the list
  • extends the list
  • reverses the list

Explanation: Let’s analyze this example:

  • the list vals is created with the integer elements 0,1,2,
  • using positive indices, the value in position 0 is swapped with the element in position 2,
  • the elements are now in this order: 2,1,0. Therefore, the list has been reversed.

More Questions: Python Essentials 1 – Module 3 Test