Applying a naming convention to code structures

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

Applying a naming convention to code structures

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which is the best name for a Python function that works out a student's average mark?
A calculate_average
B Average
2. Which is the best name for a class that stores details about one student?
A student_record
B StudentRecord
3. Which method name follows the snake_case convention?
A get_total
B GetTotal
4. A program already uses snake_case for every function. What should you name a new function that checks a password?
A checkPassword
B check_password
5. Which class name follows the convention?
A BankAccount
B bank_account

Match the Pairs

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

Left columnRight column
1def calculate_average(marks): AFunction name mixes capital letters with underscores. It should be snake_case: calculate_total.
2class StudentRecord: Bclass BankAccount:
3Function and method names use snake_case and start with a verb Cdef get_total(self):
4Class names use CamelCase nouns DClass name uses snake_case. It should be CamelCase: StudentRecord.
5def Calculate_Total(prices): EA function named in snake_case with a verb that says what it does
6class student_record: FA class named in CamelCase with a noun that says what it represents

Definitions

naming convention

Fill in the Blanks

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

1.
def _________(prices): return sum(prices) print(get_total([2, 3]))
2.
class _____________: pass record = StudentRecord()
3.
import __________ total = data_tools.get_total(marks)
4.
class Student: def ________(self): pass Student().get_name()
5.
class Student: pass s = _______()

Spot the Error

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

Question 1
A class Account:
B balance = 0
C def AddFunds(self, amt):
D self.balance += amt
Question 2
A # Tidy up a name
B from StringUtils import clean
C name = clean(' ava ')
D print(name)
Question 3
A class Teacher:
B def __init__(self):
C self.classes = []
D def classAdd(self, c):
E self.classes.append(c)
Question 4
A def get_area(w, h):
B return w * h
C def perimeter(w, h):
D return 2 * (w + h)
Question 5
A class bank_account:
B def __init__(self):
C self.balance = 0
D def get_balance(self):
E return self.balance

Answer Key

Applying a naming convention to code structures
Section A: Quick Questions
Q1
A — calculate_average
Q2
B — StudentRecord
Q3
A — get_total
Q4
B — check_password
Q5
A — BankAccount
Section B: Match the Pairs
Q1
def calculate_average(marks): → A function named in snake_case with a verb that says what it does
Q2
class StudentRecord: → A class named in CamelCase with a noun that says what it represents
Q3
Function and method names use snake_case and start with a verb → def get_total(self):
Q4
Class names use CamelCase nouns → class BankAccount:
Q5
def Calculate_Total(prices): → Function name mixes capital letters with underscores. It should be snake_case: calculate_total.
Q6
class student_record: → Class name uses snake_case. It should be CamelCase: StudentRecord.
Section C: Definitions
Q1
naming convention — A consistent rule for naming code structures: snake_case verbs for functions and methods (get_total), CamelCase nouns for classes (StudentRecord). Pick one style and stick to it.
Section D: Fill in the Blanks
Q1
def get_total(prices): return sum(prices) print(get_total([2, 3]))
Q2
class StudentRecord: pass record = StudentRecord()
Q3
import data_tools total = data_tools.get_total(marks)
Q4
class Student: def get_name(self): pass Student().get_name()
Q5
class Student: pass s = Student()
Section E: Spot the Error
Q1
C — Methods use snake_case, not CamelCase. CamelCase is for class names. It should be def add_funds(self, amt):.
Q2
B — Module names use lowercase snake_case, not CamelCase. It should be from string_utils import clean.
Q3
D — Method names should be snake_case and start with a verb. classAdd is camelCase and puts the noun first. It should be def add_class(self, c):.
Q4
C — Function names should be verbs that say what the function does. perimeter is a noun. It should be def calc_perimeter(w, h):, matching the other functions.
Q5
A — Class names use CamelCase nouns, not snake_case. It should be class BankAccount:.