Explaining data and code structures with comments

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

Explaining data and code structures with comments

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which comment best explains this list? scores = []
A scores = [] # marks for each student in Class 10A
B scores = [] # a list
2. Where is the best place for a comment that describes what a variable stores?
A Beside the variable where it is created
B Hidden at the very end of the program
3. Which comment helps a maintainer understand this record? student = {"name": "Ava", "year": 10}
A # this is where I make the student thing
B # student record: name is full name, year is year level
4. Which comment is most useful above this class attribute? self.balance = 0
A # balance: dollars currently held in the account
B # sets balance to zero
5. Which comment correctly explains a loop? for mark in scores: total += mark
A # this loop is very clever and fast
B # add each student's mark to the running total

Match the Pairs

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

Left columnRight column
1scores = [] # marks for each student AA comment beside a class that explains what the class represents
2class Student: # one student's record BThe comment only repeats the data type. It does not say what the list stores (marks for each student) or how it is used.
3A comment on an attribute states what it stores and its unit or range Cself.mark = 0 # test mark out of 100
4A comment on a loop explains what the loop does and why Dfor s in scores: # add each mark to total
5scores = [] # list EA comment beside a list that says what data it stores
6scores = [] // marks for each student FPython comments start with #, not //. The // symbol is a syntax error here, so the comment is not valid.

Definitions

scores = [] # marks for each student

Fill in the Blanks

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

1.
scores = [] _ marks for each student
2.
_____ Student: # one student and their mark def __init__(self, mark):
3.
# add every mark to the running total ___ mark in scores: total = total + mark
4.
# pass if the mark is 50 or more if mark __ 50: print("Pass")
5.
class Student: _ stores one student's mark out of 100 mark = 0

Spot the Error

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

Question 1
A scores = [] // student marks
B scores.append(85)
C print(scores)
Question 2
A ages = [14, 15, 14]
B total = sum(ages)
C n = len(ages) # total of ages
D print(total / n)
Question 3
A class Player:
B stores a player's score
C score = 0
Question 4
A marks = [70, 85, 60]
B total = 0
C for m in marks:
D total += m # print a mark
Question 5
A s = {} # one student record
B s['name'] = 'Ava' # mark
C s['mark'] = 88 # mark

Answer Key

Explaining data and code structures with comments
Section A: Quick Questions
Q1
A — scores = [] # marks for each student in Class 10A
Q2
A — Beside the variable where it is created
Q3
B — # student record: name is full name, year is year level
Q4
A — # balance: dollars currently held in the account
Q5
B — # add each student's mark to the running total
Section B: Match the Pairs
Q1
scores = [] # marks for each student → A comment beside a list that says what data it stores
Q2
class Student: # one student's record → A comment beside a class that explains what the class represents
Q3
A comment on an attribute states what it stores and its unit or range → self.mark = 0 # test mark out of 100
Q4
A comment on a loop explains what the loop does and why → for s in scores: # add each mark to total
Q5
scores = [] # list → The comment only repeats the data type. It does not say what the list stores (marks for each student) or how it is used.
Q6
scores = [] // marks for each student → Python comments start with #, not //. The // symbol is a syntax error here, so the comment is not valid.
Section C: Definitions
Q1
scores = [] # marks for each student — A comment beside a variable, list, record or class that says what it stores and how it is used, so other programmers can understand the data and code structures.
Section D: Fill in the Blanks
Q1
scores = [] # marks for each student
Q2
class Student: # one student and their mark def __init__(self, mark):
Q3
# add every mark to the running total for mark in scores: total = total + mark
Q4
# pass if the mark is 50 or more if mark >= 50: print("Pass")
Q5
class Student: # stores one student's mark out of 100 mark = 0
Section E: Spot the Error
Q1
A — Python comments start with #, not //. The // is a syntax error. It should be scores = [] # student marks.
Q2
C — The comment is misleading. len(ages) counts how many values are in the list, it does not add them up. The comment should say how many ages.
Q3
B — The comment line is missing the # symbol, so Python tries to run it as code and gives a syntax error. It should start with #.
Q4
D — The comment does not match what the loop does. total += m adds each mark to the total, it does not print anything. The comment should say add a mark.
Q5
B — The comment says mark but this key stores the student's name. A comment beside a record field must say what that field stores, so it should be # name.