Adding to a list with append()

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Adding to a list with append()

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line adds the number 5 to the end of the list scores?
A scores.append(5)
B scores.add(5)
2. What does pets look like after this code runs? pets = [] pets.append('cat')
A ['cat']
B []
3. Where does append() put the new value?
A At the start of the list
B At the end of the list
4. Which line creates an empty list ready for append()?
A names = ()
B names = []
5. What is the list after this code runs? nums = [1, 2] nums.append(3)
A [1, 2, 3]
B [3, 1, 2]

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1scores = [] Aappend() changes the list itself and gives back None, so assigning the result wipes out the list. It should be just scores.append(10).
2pets.append("cat") Bfor i in range(5): nums.append(i)
3Use append() inside a loop to build a list of the numbers 0 to 4 Cappend is a method so it needs round brackets (), not square brackets []. It should be fruit.append("apple").
4Ask the user for a name and add it to the end of the names list DAdds the value "cat" to the end of the pets list, making it one item longer
5fruit = [] fruit.append["apple"] EStarts with an empty list that has no items yet, ready to be filled with append()
6scores = [] scores = scores.append(10) Fnames.append(input("Name: "))

Definitions

list.append(value)

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
______ = [] scores.append(9)
2.
scores = [] scores.______(10)
3.
scores = [] for i in _____(5): scores.append(i)
4.
scores = [] for i in range(5): scores.append(i _ 2)
5.
scores = [] score = int(input("Score: ")) scores.append(_____)

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A scores = []
B for i in range(3):
C s = int(input("Score: "))
D scores.add(s)
E print(scores)
Question 2
A names = []
B names.append["Sam"]
C names.append("Mia")
D print(names)
Question 3
A pets = []
B pets.append("cat"
C pets.append("dog")
D print(len(pets))
Question 4
A nums = 0
B for i in range(5):
C nums.append(i)
D print(nums)
Question 5
A totals == []
B for i in range(4):
C totals.append(i * 2)
D print(totals)

Answer Key

Adding to a list with append()
Section A: Quick Questions
Q1
A — scores.append(5)
Q2
A — ['cat']
Q3
B — At the end of the list
Q4
B — names = []
Q5
A — [1, 2, 3]
Section B: Match the Pairs
Q1
scores = [] → Starts with an empty list that has no items yet, ready to be filled with append()
Q2
pets.append("cat") → Adds the value "cat" to the end of the pets list, making it one item longer
Q3
Use append() inside a loop to build a list of the numbers 0 to 4 → for i in range(5): nums.append(i)
Q4
Ask the user for a name and add it to the end of the names list → names.append(input("Name: "))
Q5
fruit = [] fruit.append["apple"] → append is a method so it needs round brackets (), not square brackets []. It should be fruit.append("apple").
Q6
scores = [] scores = scores.append(10) → append() changes the list itself and gives back None, so assigning the result wipes out the list. It should be just scores.append(10).
Section C: Definitions
Q1
list.append(value) — Adds a value to the end of a list, making it one item longer. Start with an empty list like scores = [] and use append() in a loop to build it up.
Section D: Fill in the Blanks
Q1
scores = [] scores.append(9)
Q2
scores = [] scores.append(10)
Q3
scores = [] for i in range(5): scores.append(i)
Q4
scores = [] for i in range(5): scores.append(i * 2)
Q5
scores = [] score = int(input("Score: ")) scores.append(score)
Section E: Spot the Error
Q1
D — Lists do not have an add() method. To put a value on the end of a list you use append(), so the line should be scores.append(s).
Q2
B — append is a method, so the value goes inside round brackets, not square brackets. It should be names.append("Sam").
Q3
B — The closing bracket is missing. Every append( needs a matching ). It should be pets.append("cat").
Q4
A — nums is set to the number 0, not a list, so nums.append(i) would fail. Start with an empty list: nums = [].
Q5
A — A double equals sign compares values. It does not create the list. Use a single equals sign to assign: totals = [].