🐍 Python for Everyone

← Back to exercises

Chapter 14 Practice Bank 70-90 min, hands-on

Practice Bank: OOP Basics

A deeper set of practice problems, organized by topic, for extra reps beyond the main exercises — including scenario-based problems written in the same style you'll meet in real interviews. This is the chapter where general classes become allowed: everything from Chapters 1-13 (including custom exception classes) plus class definitions, __init__, instance attributes, class attributes, instance methods, and __str__/__repr__. No @staticmethod, @classmethod, @property (Chapter 15), inheritance (Chapter 16), or any import beyond math/datetime/random/csv.

How to run these Download starter.py below and run python3 starter.py from inside the same folder.

Topic 1: Classes & Objects

1 A blueprint, two objects

Find # TODO 1.1. Write a Book class with just pass, create two separate instances, and confirm they're independent objects with is.

2 Setting attributes from outside

Find # TODO 1.2. Write a Dog class with just pass, then set .name/.breed directly on an instance from outside the class.

3 Describing an instance

Find # TODO 1.3. Write describe_instance(obj), showing an object's default string form alongside type(obj).__name__.

4 Independent instance data

Find # TODO 1.4. Write a Wallet class, set .balance on only one of two instances, and catch the AttributeError on the other.

5 Debug the Code

Find # TODO 1.5. This code assigns the same Ticket object to two names instead of creating two separate instances. Fix it.

💼 Scenario

Modeling real-world "things" as objects

Find # TODO 1.A. Create two independent SupportTicket instances with their own .subject values.

💼 Interview Prep

Class vs. object

Find # TODO 1.B. Write explain_class_vs_object(), explaining the difference between a class and an object.

Topic 2: __init__ and Instance Attributes

1 Storing constructor arguments

Find # TODO 2.1. Write a Book class with __init__(self, title, author) storing both as instance attributes.

2 A simple coordinate class

Find # TODO 2.2. Write a Point class with __init__(self, x, y) storing both.

3 A default constructor argument

Find # TODO 2.3. Write an Employee class with __init__(self, name, salary=50000), using a default argument.

4 Two stored dimensions

Find # TODO 2.4. Write a Rectangle class with __init__(self, width, height) storing both.

5 Debug the Code

Find # TODO 2.5. This __init__ assigns to a local variable name instead of self.name, losing the value. Fix it.

💼 Scenario

Building a user record at signup

Find # TODO 2.A. Write UserAccount with __init__(self, username, email).

💼 Interview Prep

What is __init__, really?

Find # TODO 2.B. Write explain_init(), explaining what __init__ actually is and when Python calls it.

Topic 3: Instance Methods & self

1 A method that changes state

Find # TODO 3.1. Write a Counter class with increment(self) that adds 1 to self.value.

2 Deposit and withdraw

Find # TODO 3.2. Write a BankAccount class with deposit(self, amount) and withdraw(self, amount) methods.

3 A computed method

Find # TODO 3.3. Write a Rectangle class with an area(self) method returning self.width * self.height.

4 A method returning a string

Find # TODO 3.4. Write a Greeter class with a greet(self) method returning a personalized string.

5 Debug the Code

Find # TODO 3.5. This method is missing self as its first parameter. Fix it.

💼 Scenario

A shopping cart's running total

Find # TODO 3.A. Write ShoppingCart with add_item(self, name, price) updating a running list and total.

💼 Interview Prep

What does self actually mean?

Find # TODO 3.B. Write explain_self(), explaining what self refers to and why every instance method needs it.

Topic 4: Class Attributes vs. Instance Attributes

1 A shared constant

Find # TODO 4.1. Write a Circle class with a shared class attribute pi and an area(self) method that uses it.

2 Shared across every instance

Find # TODO 4.2. Write an Employee class with a shared class attribute company_name, confirmed identical across instances.

3 A class-level counter

Find # TODO 4.3. Write a Product class with a class attribute total_products_created incremented in __init__.

4 Overriding on one instance

Find # TODO 4.4. Write a Vehicle class with a class attribute wheels, then override it on one instance and show the class attribute is untouched.

5 Debug the Code (the mutable class attribute gotcha)

Find # TODO 4.5. This Cart class defines items = [] at the class level instead of inside __init__, so every instance shares one list. Fix it.

💼 Scenario

A shared interest rate across all accounts

Find # TODO 4.A. Write SavingsAccount with a class-level interest_rate applied in apply_interest(self).

💼 Interview Prep

The mutable class attribute bug

Find # TODO 4.B. Write explain_mutable_class_attribute_bug(), explaining the bug and why it happens.

Topic 5: __str__ and __repr__

1 A friendly print

Find # TODO 5.1. Write a Book class with a __str__(self) method used automatically by print().

2 An unambiguous developer view

Find # TODO 5.2. Write a Point class with a __repr__(self) method used automatically by repr().

3 Both at once

Find # TODO 5.3. Write a Temperature class with both __str__ and __repr__, showing they can differ.

4 Formatting in __str__

Find # TODO 5.4. Write a Money class with a __str__(self) method formatting an amount to 2 decimal places.

5 Debug the Code

Find # TODO 5.5. This class's display method is misspelled __string__ instead of __str__. Fix it.

💼 Scenario

Friendly logging output

Find # TODO 5.A. Write OrderEvent with a __str__(self) producing a clean log line.

💼 Interview Prep

__str__ vs. __repr__

Find # TODO 5.B. Write explain_str_vs_repr(), explaining the difference between the two and when each is used.

Topic 6: Bringing It Together — OOP Basics in Production

1 Combining a method and __str__

Find # TODO 6.1. Write an InventoryItem class combining an instance method, __str__, and a computed value.

2 Auto-incrementing ids

Find # TODO 6.2. Write a Task class using a class-attribute counter (next_id) to auto-assign unique ids.

3 A per-instance collection

Find # TODO 6.3. Write a Playlist class with its own fresh instance list of songs and a method-based song count.

4 Safely handling an empty case

Find # TODO 6.4. Write a TemperatureLog class with an average(self) method that safely handles an empty list.

5 Debug the Code

Find # TODO 6.5. This Team class's members = [] was a shared class attribute instead of a per-instance list. Fix it.

💼 Scenario

A small order-processing system

Find # TODO 6.A. Write Order combining line items, a running total, and a status change into one object.

💼 Interview Prep

Why classes, not just dicts and functions?

Find # TODO 6.B. Write explain_oop_basics_production_value(), describing why real production codebases organize data and behavior into classes.

Checking your work Compare your output against solution.py. Your exact wording doesn't need to match — the goal is that your program runs without errors and does what each TODO asks.
← Exercises Challenges →