Python Foundations·Practice drills 18

Drill 28: Change a value — Classes and dataclasses

Describe what would change if you edited one value.

Beginner 5 min read Lesson 460 of 480 Challenge included
What you will be able to do
  • Describe what would change if you edited one value.

Practice task

Describe what would change if you edited one value.

classes.pyPython
from dataclasses import dataclass, field

@dataclass
class Learner:
    name: str
    scores: list[int] = field(default_factory=list)

    def average(self) -> float:
        return sum(self.scores) / len(self.scores) if self.scores else 0.0

    def passed(self, mark: float = 75) -> bool:
        return self.average() >= mark

ada = Learner("Ada", [88, 92, 79])
print(ada)
print(f"{ada.average():.1f}")
print(ada.passed())
Your turn

Write your answer

Easy

Describe what would change if you edited one value.

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 460 of 480 · 0% of course