Accessing record fields by index

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

Accessing record fields by index

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. student = (101, "Mia", 16, 87.5) Which index gives the name "Mia"?
A student[1]
B student[2]
2. student = (101, "Mia", 16, 87.5) What does student[0] return?
A 101 (the ID)
B "Mia" (the name)
3. Which statement about reading a tuple record is true?
A You read a field by typing its field name, like student["name"]
B You must know which index holds each field
4. student = (101, "Mia", 16, 87.5) Which line prints the age?
A print(student[3])
B print(student[2])
5. student = (101, "Mia", 16, 87.5) What happens when you run print(student[4])?
A An IndexError, because the last index is 3
B It prints 87.5, the last field

Match the Pairs

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

Left columnRight column
1student = (101, "Ava", 15, 88.5) print(student[1]) Astudent = (101, "Ava", 15, 88.5)
2student[0] Bmark = student[3]
3Fields in a tuple record are read by index position, not by name, so you must remember the field order CA tuple record is read by index number, not by field name. Use student[1] to get the name.
4One record can mix data types, so each index holds a different type: int, string, int, float DThe ID is the first field, so it sits at index 0, not 1. Indexes start at 0, so use student[0].
5student = (101, "Ava", 15, 88.5) print(student["name"]) EPrints "Ava", the second field, because index 1 is the second position in the tuple record
6id = student[1] FReads the first field of the record, which is the ID in this record layout

Definitions

student[1]

Fill in the Blanks

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

1.
student = (101, "Ava", 15, 88.5) print(student[_]) # the ID
2.
student = (101, "Ava", 15, 88.5) name = student[_]
3.
student = (7, "Liam", 16, 72.5) print(_______[2])
4.
student = (101, "Ava", 15, 88.5) mark = student[_]
5.
ID, NAME, SCORE = 0, 1, 2 student = (5, "Zoe", 91.5) print(student[_____])

Spot the Error

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

Question 1
A student = (101, "Ava", 15, 88.5)
B name = student(1)
C print(name)
Question 2
A student = (7 "Liam", 16, 92.0)
B print(student[0])
C print(student[1])
Question 3
A student = (12, "Zoe", 14, 77.5)
B student_id = student[0]
C age = student[3]
D print(student_id, age)
Question 4
A student = (5, "Noah", 15, 63.0)
B print(student["name"])
C print(student[3])
Question 5
A student = ("Mia", 9, 17, 95.5)
B print("ID:", student[0])
C print("Name:", student[1])

Answer Key

Accessing record fields by index
Section A: Quick Questions
Q1
A — student[1]
Q2
A — 101 (the ID)
Q3
B — You must know which index holds each field
Q4
B — print(student[2])
Q5
A — An IndexError, because the last index is 3
Section B: Match the Pairs
Q1
student = (101, "Ava", 15, 88.5) print(student[1]) → Prints "Ava", the second field, because index 1 is the second position in the tuple record
Q2
student[0] → Reads the first field of the record, which is the ID in this record layout
Q3
Fields in a tuple record are read by index position, not by name, so you must remember the field order → mark = student[3]
Q4
One record can mix data types, so each index holds a different type: int, string, int, float → student = (101, "Ava", 15, 88.5)
Q5
student = (101, "Ava", 15, 88.5) print(student["name"]) → A tuple record is read by index number, not by field name. Use student[1] to get the name.
Q6
id = student[1] → The ID is the first field, so it sits at index 0, not 1. Indexes start at 0, so use student[0].
Section C: Definitions
Q1
student[1] — Reads a field from a record stored as a tuple by its index position. Indexes start at 0, so student[0] is the first field. You must know which field sits at which index.
Section D: Fill in the Blanks
Q1
student = (101, "Ava", 15, 88.5) print(student[0]) # the ID
Q2
student = (101, "Ava", 15, 88.5) name = student[1]
Q3
student = (7, "Liam", 16, 72.5) print(student[2])
Q4
student = (101, "Ava", 15, 88.5) mark = student[3]
Q5
ID, NAME, SCORE = 0, 1, 2 student = (5, "Zoe", 91.5) print(student[SCORE])
Section E: Spot the Error
Q1
B — Fields of a tuple record are read with square brackets, not round brackets. It should be name = student[1].
Q2
A — A comma is missing between the first two fields, so the record is not valid. It should be (7, "Liam", 16, 92.0).
Q3
C — Index 3 holds the mark (77.5), not the age. Age is the third field, which sits at index 2, so it should be student[2].
Q4
B — A tuple record is read by index position, not by field name. The name is the second field, so use student[1].
Q5
A — The fields are in the wrong order. The ID must be first so that student[0] is the ID and student[1] is the name. It should be (9, "Mia", 17, 95.5).