Back to the topic

The mark scheme starts on a new page, so you can print pages one and two for a student and keep the last page yourself. Everything prints in black on white whichever theme you are using.

Python · 2.3

Lists

Section A: 10 multiple choice questions, 10 marks. Section B: 5 written questions, 17 marks. Total 27 marks.

Name

Class

Date

Mark  / 27

Section A

Circle the letter of the correct answer. One mark each.

  1. 1What does scores[-1] give?1
    1. AThe length
    2. BAn error
    3. CThe first item
    4. DThe last item
  2. 2What does scores[1:3] give for [45, 78, 12, 90]?1
    1. A[78, 12, 90]
    2. B[45, 78]
    3. C[78, 12]
    4. D[78]
  3. 3What does len([4, 8, 15]) give?1
    1. A2
    2. B27
    3. C3
    4. DAn error
  4. 4How do you add an item to the end of a list?1
    1. Ainsert()
    2. Bappend()
    3. Cadd()
    4. Dpush()
  5. 5What does scores.append(5) do?1
    1. ASorts the list
    2. BRemoves 5
    3. CInserts 5 at the start
    4. DAdds 5 to the end of the list
  6. 6What is the difference between sort() and sorted()?1
    1. Asorted() only works on strings
    2. Bsort() changes the list, sorted() returns a new sorted copy
    3. CThey are identical
    4. Dsorted() changes the list, sort() returns a copy
  7. 7What does numbers[::-1] give?1
    1. AThe list reversed
    2. BThe first item
    3. CThe last item
    4. DAn error
  8. 8What is the difference between remove(3) and del numbers[3]?1
    1. Aremove deletes the value 3, del deletes the item at index 3
    2. Bdel only works on strings
    3. CThey are identical
    4. Dremove works on index, del works on value
  9. 9After list2 = list1, what does changing list2 do?1
    1. AIt raises an error
    2. BIt changes list1 too, because both names refer to the same list
    3. CNothing, list1 is a copy
    4. DIt only changes list2
  10. 10What does [n * 2 for n in [1, 2, 3]] produce?1
    1. A[1, 4, 9]
    2. B[1, 2, 3]
    3. C[2, 4, 6]
    4. D6

Section B

Answer in the space provided. The number of marks tells you how many separate points to make.

  1. 1ExplainExplain the difference between the list methods sort() and the function sorted().3
  2. 2WriteWrite a program that stores five marks in a list and prints the highest, the lowest and the mean.5
  3. 3ExplainExplain the difference between numbers.remove(3) and del numbers[3].3
  4. 4ExplainA programmer writes copy = original and then appends to copy. Explain why original changes as well.3
  5. 5ExplainA program uses numbers[5] on a list containing five items and crashes. Explain why and state the valid range of indexes.3

Mark scheme

Section A

1D2C3C4B5D6B7A8A9B10C

Section B

1 Explain the difference between the list methods sort() and the function sorted(). 3

One mark for each of the following, to a maximum of 3.

  • sort() rearranges the original list in place
  • sorted() returns a new sorted list
  • Use sorted() when the original order must be kept

Model answer. The sort method rearranges the items of the list it is called on, changing the original list permanently and returning nothing. The sorted function leaves the original list completely untouched and returns a brand new list containing the same items in order. Which one to use depends on whether the original order still matters: if it does, sorted must be used, otherwise the original ordering is lost and cannot be recovered.

2 Write a program that stores five marks in a list and prints the highest, the lowest and the mean. 5

One mark for each of the following, to a maximum of 5.

  • Creates a list containing the five marks
  • Uses max to find the highest
  • Uses min to find the lowest
  • Uses sum divided by len for the mean
  • Prints all three results

Model answer. marks = [67, 82, 45, 91, 58] print(f"Highest: {max(marks)}") print(f"Lowest: {min(marks)}") print(f"Mean: {sum(marks) / len(marks):.1f}") The built in functions max, min, sum and len all take the list directly, so no loop is needed at all. The mean is the total divided by how many items there are, and len is used rather than typing 5 so that the program still works correctly if marks are added or removed later.

3 Explain the difference between numbers.remove(3) and del numbers[3]. 3

One mark for each of the following, to a maximum of 3.

  • remove searches for the value 3 and deletes the first match
  • del deletes whatever item is at index 3
  • They affect different items unless the value happens to equal the index

Model answer. The remove method takes a value rather than a position. It searches the list from the start and deletes the first item equal to that value, raising a ValueError if no item matches. The del statement takes a position and removes whatever item currently sits at that index, raising an IndexError if the index does not exist. For the list [10, 20, 3, 40], remove(3) deletes the value 3 while del numbers[3] deletes 40, so confusing the two silently removes the wrong item.

4 A programmer writes copy = original and then appends to copy. Explain why original changes as well. 3

One mark for each of the following, to a maximum of 3.

  • Assignment copies the reference, not the list itself
  • Both names therefore refer to one single list in memory
  • Use copy() or list() to make a genuine copy

Model answer. Assigning one list to another name does not duplicate the list. It makes the new name refer to exactly the same list already held in memory, so there is only ever one list with two labels attached to it. Appending through either name therefore changes what is seen through both. To create a genuinely separate list the programmer must copy the contents, using original.copy(), list(original) or the slice original[:], each of which builds a new list containing the same items.

5 A program uses numbers[5] on a list containing five items and crashes. Explain why and state the valid range of indexes. 3

One mark for each of the following, to a maximum of 3.

  • List indexes start at 0
  • Five items therefore occupy indexes 0 to 4
  • Index 5 is out of range so an IndexError occurs

Model answer. Python lists are indexed from 0, so the first item is at index 0 rather than index 1. A list containing five items therefore occupies indexes 0, 1, 2, 3 and 4, and there is no item at index 5. Attempting to access it raises an IndexError and the program stops. The highest valid index is always the length of the list minus one, which is why loops over a list use range(len(numbers)) rather than range(1, len(numbers) + 1).