Accessing a record's fields by name and by field index
Gets a field from a record by its field name. A list or tuple record uses the field index (its position) instead, such as student[0]. Indexes start at 0.
Understanding Accessing a record's fields by name and by field index in VCE Software Development U3O1
What it does: Round brackets create a tuple. Its fields are read by index, but student[1] = 18 would cause an error. Unpacking assigns the fields in order to the variables, so name gets student[0], age gets student[1] and year gets student[2]. A dictionary record is accessed by its field name written as a string in quotes. Without quotes, Python looks for a variable called name. Field indexes start at 0, so the first field is at index 0. Python counts positions from 0. The field at index 1 is the second field. Unpacking matches each field to a variable by position. The second line gives all three variables the whole tuple.
Common mistakes: A dictionary record's field name must be written as a string in quotes. Without quotes, Python looks for a variable called name and raises a NameError. It should be print(s['name']). The tuple has 3 fields, so the field indexes are 0, 1 and 2. Index 3 is out of range and causes an IndexError. The house is at index 2: print(student[2]).
Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.
This module's key terms are also in a printable crossword (Modules 98–113) and word search (Modules 72–113).
Take this worksheet further — free with Cloodit
Each concept is reinforced from every angle — learned, then applied, then tested.
Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.
Over 100 of them, across seven natural biomes.
Turns their work into revision material ready for Flash Cards and Exam study.
Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.
Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.
Quick Questions
Match the Pairs
Key Term
student['name']Fill in the Blanks
Spot the Error
Student with a class code? Join your class →