Welcome to your ninth homework! Last week you built your first classes. This
week we make those objects do much more: print nicely (__str__ /
__repr__), compare with == (__eq__), share data across every object
(class attributes), reuse each other's code (inheritance and super()), and
expose computed values as if they were plain attributes (@property). Several
tasks build on last week's classes — but now your objects are far more powerful.
Deadline: before Workshop 10.
How to submit: push everything to the same GitHub repository you used for the previous homeworks, then re-send the repository link to your instructor.
Put this homework in its own folder so it does not mix with the earlier ones:
cd python-homework # the repo from Homework 1
mkdir workshop_9_homework
cd workshop_9_homework
# ...create your exercise files in here...When you are done, commit and push from the repository folder:
git add .
git commit -m "Add workshop 9 homework"
git pushIf a command fails, read the error message carefully — it usually tells you what is wrong. If you are still stuck, take a screenshot and bring it to the next workshop.
Create the files below and write Python code to solve each task. Use everything we
have covered so far: variables, f-strings, input(), int() / float(),
if / elif / else, and / or / not, while, for + range(), +=,
len(), string methods, lists, dictionaries, tuples, files (open), try /
except, comprehensions, modules (import), and classes (class, __init__,
self, methods) — plus this week's new material:
__str__/__repr__— special ("dunder") methods that decide how your object prints.__str__is the friendly text forprint();__repr__is the exact, developer-facing text used in the REPL and inside lists. Both return a string.__eq__— decide when two objects count as equal:def __eq__(self, other):returnsTrue/Falseby comparing their data. Now==andinwork on your type.- Class attributes — a value written straight in the class body (not on
self) is shared by every object. Perfect for constants and for counting how many objects exist:Thing.count += 1inside__init__. - Inheritance —
class Dog(Animal):makesDoga kind ofAnimal; it gets all ofAnimal's methods and can override any of them. super().__init__(...)— inside a subclass's__init__, run the parent's setup first, then add the subclass's own attributes.@property— turn a method into a computed attribute you read without parentheses:rect.areainstead ofrect.area().
Run each file with:
python exercise_1.pyDefine a class Book with a title and an author. Add a __str__(self) method
that returns a sentence like "Python 101 by nino". Build two books and
print each one.
Example run:
Python 101 by nino
Clean Code by giorgi
Hint:
def __str__(self):thenreturn f"{self.title} by {self.author}". Once__str__exists,print(book)uses it automatically.
Define a class Coordinate with an x and a y. Add __eq__(self, other) so
that two coordinates are equal when both parts match. Print the result of
comparing an equal pair and an unequal pair.
Example run:
True
False
Hint:
def __eq__(self, other): return self.x == other.x and self.y == other.y. Builda = Coordinate(3, 7),b = Coordinate(3, 7),c = Coordinate(9, 0), thenprint(a == b)andprint(a == c).
Give a class (say Robot) a class attribute count = 0 written in the class
body. In __init__, do Robot.count += 1. Build three robots, then print how
many were made using Robot.count.
Example run:
Made 3 robots.
Hint:
count = 0goes directly underclass Robot:— not onself. Each new robot bumps the shared counter withRobot.count += 1.
Write a base class Animal with a method speak(self) that returns a generic
sound. Write two subclasses (for example Dog and Cat) that override
speak. Build one of each and print what each says.
Example run:
Rex says woof
Whiskers says meow
Hint:
class Dog(Animal):puts the parent in the parentheses. GiveAnimalan__init__that storesself.name, and let the subclasses reuse it — onlyspeakneeds to change.
Write a base class Vehicle with __init__(self, brand). Write a subclass Car
whose __init__ also takes doors, calls super().__init__(brand) to reuse
the parent's setup, and then stores self.doors. Print a car's brand and doors.
Example run:
Toyota with 4 doors
Hint:
class Car(Vehicle):thendef __init__(self, brand, doors): super().__init__(brand); self.doors = doors. After that,self.brandexists (the parent set it) and so doesself.doors.
Write a class Rectangle with a width and a height. Add an @property called
area that returns width * height. Build a rectangle and print its area —
with no parentheses (rect.area, not rect.area()).
Example run:
Area: 12
Hint: put
@propertyon the line directly abovedef area(self):. Then read it like an attribute:print(f"Area: {rect.area}").
Write a base class Shape and two subclasses Square and Circle. Each subclass
stores what it needs (a side, or a radius) and has its own @property area and a
__str__. Put a few shapes in a list and print each one.
Example run:
Square with area 16
Circle with area 28.27
Hint: give
Shapea__str__that usesself.area, and let each subclass fill in its ownarea. A circle's area ismath.pi * r * r(import math). Format to two decimals withf"{self.area:.2f}".
Combine classes, json, and error handling. Write a class Library
whose __init__ tries to json.load a list of books from library.json (each
book is a small dict like {"title": ..., "author": ...}); catch
FileNotFoundError and start with an empty list if the file is missing. Add a
method add(self, title, author) that appends a book dict and saves the whole
list back with json.dump, and a method show(self) that prints every book as
"title by author". Run the program two or three times, adding a book each time,
and watch the shelf grow.
Example first run:
0 book(s) on the shelf.
Add a title: The Hobbit
Add an author: tolkien
Saved!
Example second run:
1 book(s) on the shelf.
- The Hobbit by tolkien
Add a title: Dune
Add an author: herbert
Saved!
Hint: in
__init__, wrap the read intry: ... except FileNotFoundError: self.books = []. Inadd, doself.books.append({"title": title, "author": author}), then re-openlibrary.jsonin"w"mode andjson.dump(self.books, f).
-
exercise_1.py…exercise_8.pywritten and run without errors usingpython exercise_N.py - Everything is pushed inside the
workshop_9_homework/folder of your repository - You re-sent your repository link to the instructor
- Classes (tutorial): https://docs.python.org/3/tutorial/classes.html
- Inheritance: https://docs.python.org/3/tutorial/classes.html#inheritance
super(): https://docs.python.org/3/library/functions.html#super- Special (dunder) methods, incl.
__str__/__repr__/__eq__: https://docs.python.org/3/reference/datamodel.html#special-method-names __str__vs__repr__: https://docs.python.org/3/reference/datamodel.html#object.__repr__@property: https://docs.python.org/3/library/functions.html#propertyjsonmodule: https://docs.python.org/3/library/json.html