Storing objects in a list

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

Storing objects in a list

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line adds a Student object called s1 to a list named students?
A students.append(s1)
B students.add(s1)
2. Which loop correctly calls the show() method on every object in the list pets?
A for pet in pets: pet.show()
B for pet in pets: pets.show()
3. True or false: A single list can store many objects that were created from the same class.
A True
B False
4. Which code creates a Car object and stores it in the list cars in one step?
A cars = Car("Mazda", 2019).append()
B cars.append(Car("Mazda", 2019))
5. You want to find the Student whose name is "Mia" in the list students. Which test inside the loop is correct?
A if s.name = "Mia":
B if s.name == "Mia":

Match the Pairs

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

Left columnRight column
1pets.append(Pet("Rex")) AThe method is called without an object and is not indented inside the loop. It should be indented and written as p.speak().
2for p in pets: p.speak() Bpets = []
3Create an empty list ready to store many objects CThe closing bracket for append() is missing, so there is a syntax error. It needs two closing brackets at the end.
4Find an object in the list by checking one of its attributes Dif p.name == "Rex":
5pets.append(Pet("Rex") ECreates a new Pet object and adds it to the end of the pets list
6for p in pets: speak() FLoops over every object in the pets list and calls its speak() method

Definitions

objects.append(obj)

Fill in the Blanks

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

1.
students = [] students.______(Student("Ava", 7))
2.
___ s in students: s.show()
3.
for pet in pets: ___.speak()
4.
for s in students: if s.name __ "Ava": print(s.points)
5.
students = [] students.append(Student("Liam", 8)) print(___(students))

Spot the Error

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

Question 1
A pets = ()
B pets.append(Pet("Rex"))
C for p in pets:
D p.speak()
Question 2
A dogs = []
B dogs.append(Dog("Rex"))
C dogs.append(Dog("Bo"))
D for d in dogs
E d.speak()
Question 3
A cars = []
B cars.append(Car("Red"))
C for c in Car:
D c.drive()
Question 4
A players = []
B players.add(Player("Ana"))
C for p in players:
D print(p.name)
Question 5
A scores = []
B for n in [5, 8, 9]:
C scores.append(Score)
D for s in scores:
E s.show()

Answer Key

Storing objects in a list
Section A: Quick Questions
Q1
A — students.append(s1)
Q2
A — for pet in pets: pet.show()
Q3
A — True
Q4
B — cars.append(Car("Mazda", 2019))
Q5
B — if s.name == "Mia":
Section B: Match the Pairs
Q1
pets.append(Pet("Rex")) → Creates a new Pet object and adds it to the end of the pets list
Q2
for p in pets: p.speak() → Loops over every object in the pets list and calls its speak() method
Q3
Create an empty list ready to store many objects → pets = []
Q4
Find an object in the list by checking one of its attributes → if p.name == "Rex":
Q5
pets.append(Pet("Rex") → The closing bracket for append() is missing, so there is a syntax error. It needs two closing brackets at the end.
Q6
for p in pets: speak() → The method is called without an object and is not indented inside the loop. It should be indented and written as p.speak().
Section C: Definitions
Q1
objects.append(obj) — Adds an object to the end of a list so many objects are stored together. You can then loop over the list with a for loop to call each object's methods or find one by its attribute.
Section D: Fill in the Blanks
Q1
students = [] students.append(Student("Ava", 7))
Q2
for s in students: s.show()
Q3
for pet in pets: pet.speak()
Q4
for s in students: if s.name == "Ava": print(s.points)
Q5
students = [] students.append(Student("Liam", 8)) print(len(students))
Section E: Spot the Error
Q1
A — A tuple () cannot be added to, so append() would fail. Create an empty list with pets = [].
Q2
D — A for loop header must end with a colon. It should be for d in dogs:
Q3
C — The loop must go over the list of objects, not the class name. It should be for c in cars:
Q4
B — Lists do not have an add() method. Use players.append(...) to store the object in the list.
Q5
C — This appends the class itself, not an object. Create an object with Score(n) so show() works later.