Python Foundations·Practice drills 3

Drill 5: Why this way — Functions and collections

Explain why the example is written this way rather than another way.

Beginner 5 min read Lesson 87 of 480 Challenge included
What you will be able to do
  • Explain why the example is written this way rather than another way.

Practice task

Explain why the example is written this way rather than another way.

collections.pyPython
def average(values):
    if not values:
        return 0
    return sum(values) / len(values)

scores = {"html": 88, "css": 92, "js": 79}

for skill, score in scores.items():
    print(f"{skill.upper()}: {score}")

passed = [skill for skill, score in scores.items() if score >= 80]
print(f"Passed: {passed}")
print(f"Average: {average(list(scores.values())):.1f}")
Your turn

Write your answer

Easy

Explain why the example is written this way rather than another way.

TEXT · starter
Your answer…
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Lesson 87 of 480 · 0% of course