Checkpoint: Python Essentials

Published by Brayan K

You've learned string formatting, string methods, sets, tuples, comprehensions, *args/**kwargs, lambda, enumerate and zip — let's combine them. This checkpoint isn't a new topic; it's where the separate pieces click into a single, working program.

Part of the free Python course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

Sorting pairs needs a key

The program this checkpoint builds is a class scoreboard, made from (name, score) pairs. First, four places where the pieces catch people out when they meet. Sorting a list of pairs without a key sorts them by their first item, here the name, because tuples compare item by item, as Tuples showed. To sort by score, give sorted() a key, as in Lambda Functions:

passers = [("Ada", 88), ("Cleo", 95), ("Dan", 72), ("Finn", 80)]
print(sorted(passers))
print(sorted(passers, key=lambda s: s[1], reverse=True))

# ✅ Expected output:
# [('Ada', 88), ('Cleo', 95), ('Dan', 72), ('Finn', 80)]
# [('Cleo', 95), ('Ada', 88), ('Finn', 80), ('Dan', 72)]

A set of grade bands has no order

A set comprehension, from Comprehensions, collects each grade band once. Printing the set itself would show the bands in a different order from one run to the next, as Sets explained, so sort it, or ask it questions with len() and in:

def grade(score):
    if score >= 90:
        return "A"
    elif score >= 75:
        return "B"
    return "C"

ranked = [("Cleo", 95), ("Ada", 88), ("Finn", 80), ("Dan", 72)]
bands = {grade(score) for name, score in ranked}
print(len(bands), sorted(bands))
print("B" in bands, "D" in bands)

# ✅ Expected output:
# 3 ['A', 'B', 'C']
# True False

When nobody passes

The top scorer is the first pair of the sorted list, unpacked into two names. That works until the filter keeps nobody, and there's no first pair to read:

students = [("Ben", 54), ("Eve", 41)]
passers = [s for s in students if s[1] >= 60]
print(passers)
top_name, top_score = passers[0]

# ✅ Expected output:
# []

# ✅ Expected error:
# IndexError: list index out of range

An empty list counts as false in an if, so check before reading:

students = [("Ben", 54), ("Eve", 41)]
passers = [s for s in students if s[1] >= 60]
if passers:
    top_name, top_score = passers[0]
    print("Top:", top_name, top_score)
else:
    print("Nobody passed")

# ✅ Expected output:
# Nobody passed

Averages from three lists

When the scores arrive as separate lists, one per subject, zip() lines them up, a function with *scores averages however many it's given, a dictionary comprehension keeps each name's average, and max() with a key finds the best one:

def average(*scores):
    return round(sum(scores) / len(scores), 1)

names = ["Ada", "Ben", "Cleo"]
maths = [88, 54, 95]
science = [92, 61, 89]

report = {name: average(m, s) for name, m, s in zip(names, maths, science)}
for name, avg in report.items():
    print(f"{name:<6} avg {avg}")

best = max(report.items(), key=lambda item: item[1])
print()
print(f"Top average: {best[0]} ({best[1]})")

# ✅ Expected output:
# Ada    avg 90.0
# Ben    avg 57.5
# Cleo   avg 92.0
#
# Top average: Cleo (92.0)

The pieces come from enumerate & zip, *args & **kwargs, Comprehensions and Lambda Functions. max() compares the pairs by their second item and hands back the whole pair, so best[0] is the name and best[1] the average.

Practice: the passers, in order

Fill the three blanks with the values in the hint lines, in order: keep the scores of 60 or more, sort by the score, and number the lines from 1.

students = [("Ada", 88), ("Ben", 54), ("Cleo", 95), ("Dan", 72)]
passers = [s for s in students if s[1] ___ 60]
ranked = sorted(passers, key=lambda s: s[___], reverse=True)
for rank, (name, score) in ___(ranked, start=1):
    print(f"{rank}. {name} ({score})")

# 👉 replace ___ with >=
# 👉 replace ___ with 1
# 👉 replace ___ with enumerate

# ✅ Expected output:
# 1. Cleo (95)
# 2. Ada (88)
# 3. Dan (72)

On your own: the scoreboard

Now put the pieces together. Starting from the six students in the editor below:

students = [
    ("Ada", 88), ("Ben", 54), ("Cleo", 95),
    ("Dan", 72), ("Eve", 41), ("Finn", 80),
]
PASS_MARK = 60

def grade(score):
    if score >= 90:
        return "A"
    elif score >= 75:
        return "B"
    return "C"

# Your code here: the four steps above

Four students pass, Cleo first, and the bands awarded are A, B and C. Then compare with this solution:

students = [
    ("Ada", 88), ("Ben", 54), ("Cleo", 95),
    ("Dan", 72), ("Eve", 41), ("Finn", 80),
]
PASS_MARK = 60

def grade(score):
    if score >= 90:
        return "A"
    elif score >= 75:
        return "B"
    return "C"

passers = [(name, score) for name, score in students if score >= PASS_MARK]
ranked = sorted(passers, key=lambda s: s[1], reverse=True)

print("=== SCOREBOARD ===")
print(f"{'#':<3}{'Name':<8}{'Score':>6}  Grade")
for rank, (name, score) in enumerate(ranked, start=1):
    print(f"{rank:<3}{name:<8}{score:>6}  {grade(score)}")

bands = {grade(score) for name, score in ranked}
top_name, top_score = ranked[0]
print("-" * 25)
print(f"Passed: {len(ranked)}/{len(students)}")
print(f"Grade bands awarded: {', '.join(sorted(bands))}")
print(f"Top scorer: {top_name} with {top_score}")

# ✅ Expected output:
# === SCOREBOARD ===
# #  Name     Score  Grade
# 1  Cleo        95  A
# 2  Ada         88  B
# 3  Finn        80  B
# 4  Dan         72  C
# -------------------------
# Passed: 4/6
# Grade bands awarded: A, B, C
# Top scorer: Cleo with 95

A comprehension filters, a lambda drives the sort, enumerate() numbers the rows, unpacking reads each pair, f-string widths line up the columns, and a set comprehension collects the bands, which sorted() and join() turn into one line of text. The top scorer can be read without a check here, because four students passed; with data that could leave nobody, check first, as above.

Practice quiz

What does f"Total: {7:.2f}" give?

  • Total: 7
  • Total: 7.0
  • Total: 7.00
  • Total: {7:.2f}

Answer: Total: 7.00. .2f shows two digits after the point, even for a whole number. (f-strings & String Formatting)

What does " Hi-There ".strip().lower().replace("-", " ") give?

  • hi there
  • Hi There
  • hi there
  • hi-there

Answer: hi there. Each method works on the text the one before it made: trimmed, then lower case, then the hyphen swapped. (String Methods)

What does {1, 2, 3} & {3, 4, 5} give?

  • {1, 2, 3, 4, 5}
  • {3}
  • {1, 2}
  • {4, 5}

Answer: {3}. & keeps the values that are in both sets. (Sets)

x, y = (10, 20), then print(x + y). What prints?

  • (10, 20)
  • 1020
  • An error
  • 30

Answer: 30. Unpacking puts 10 in x and 20 in y. (Tuples)

What does [n for n in range(6) if n % 2 == 0] give?

  • [0, 2, 4]
  • [1, 3, 5]
  • [2, 4, 6]
  • [0, 1, 2, 3, 4, 5]

Answer: [0, 2, 4]. The filter keeps the even numbers from 0 to 5. (Comprehensions)

for i, n in enumerate(["Ada", "Ben"], start=1): print(i, n). What is the first line?

  • 0 Ada
  • 1 Ada
  • Ada 1
  • 1 Ben

Answer: 1 Ada. start=1 makes the count begin at 1, and the count comes first. (enumerate & zip)

def f(*args): return args, then print(f(1, 2)). What prints?

  • [1, 2]
  • 1 2
  • (1, 2)
  • An error

Answer: (1, 2). *args collects the positional arguments into a tuple. (*args & **kwargs)

What does sorted([("a", 2), ("b", 1)], key=lambda p: p[1]) give?

  • [('a', 2), ('b', 1)]
  • [2, 1]
  • [1, 2]
  • [('b', 1), ('a', 2)]

Answer: [('b', 1), ('a', 2)]. The key sorts the pairs by their second item and returns the pairs themselves. (Lambda Functions)

Continue this course

Frequently asked questions

Which lessons does this checkpoint cover?

The eight lessons before it: *args & **kwargs, Lambda Functions, f-strings & String Formatting, String Methods, Sets, Tuples, Comprehensions, and enumerate & zip. Each step of the scoreboard names the lesson it comes from, so a step that stumps you points to the lesson to reread.

Should I be able to do the scoreboard without looking?

Try each step on your own first, then compare with the solution. Struggling a little before you look is how the pieces stick. If one step won't come, reread its lesson rather than the solution, then try the step again.

My solution is different from the one shown. Is it wrong?

Not if it prints the same scoreboard. A loop with an if instead of the comprehension, or a named function instead of the lambda, gives the same result; the solution shows one short way. Compare the output line by line.

Related lessons