Chapters
Python114 chapters

Exercises

Classes and Objects

3 tasks. Write the code, press Check, and the page runs it against a real Python interpreter.

Exercise 1

Define a Dog class with a shared species attribute, build one, and print the species.

Python
# define Dog with species, then build one and print it
Exercise 2

Both dogs share one tricks list. Show it: append through one and print the other's.

Python
class Dog:
    tricks = []

rex = Dog()
fido = Dog()
# append through rex, then print fido's
Exercise 3

Objects are mutable, so a function can change one. Make bump add one to the counter.

Python
class Counter:
    pass

def bump(c):
    # add one to c.value
    pass

counter = Counter()
counter.value = 0
bump(counter)
bump(counter)
print(counter.value)