livepython.net
CHAPTER 20Object-Oriented Programming (Classes)
20.6  PRACTICE

Python OOP Practice Questions for Beginners

Chapter 20 Recap and Practice

You can now build your own kinds of objects. Here is the chapter in one table:

You writeIt does
class Dog:defines a class: a blueprint, named in PascalCase
rocky = Dog()creates an object (an instance) from the class
rocky.name = "Rocky"stores an attribute in that one object
type(rocky)shows the object's class: <class '__main__.Dog'>
isinstance(rocky, Dog)True if the object was made from that class (or a child of it)
def __init__(self, name):runs by itself for each new object and sets its attributes
self.name = nameself is the object itself; this stores the value inside it
def __init__(self, owner, balance=0):a default value for an attribute
def bark(self):a method: a function inside the class
rocky.bark()calls the method; Python passes rocky as self
return self.width * self.heighta method that answers a question about its object
raise ValueError("Not enough money")a method that protects its object from a bad change
def __str__(self):returns the text that print(), str() and f-strings show
class Dog(Animal):inheritance: the child gets every method of the parent
a method with the parent's nameoverrides it: Python looks in the child first
super().__init__(name)runs the parent's __init__, then the child adds its own attributes

And the habits: create every attribute in __init__, change attributes through methods, return a string from __str__, and use inheritance only when the child is a kind of the parent.

Practice questions

Write each program with at least one class, and use its attributes and methods. Use input() with no prompt text. The tests try several inputs, so test your program with a few of your own too.

Classes and objects

  1. 180.Python Program to Create a Class and an ObjectEasySolve
  2. 181.Python Program to Find the Older Pet Using ObjectsMediumSolve

The __init__ method and attributes

  1. 182.Python Program to Make a Student Class with __init__EasySolve
  2. 183.Python Program to Track a Game Character's HealthMediumSolve

Methods

  1. 184.Python Program to Find Area and Perimeter Using a ClassEasySolve
  2. 185.Python Program to Make a Bank Account ClassMediumSolve

The __str__ method

  1. 186.Python Program to Print a Book Object Using __str__EasySolve
  2. 187.Python Program to Print a Shopping Cart of ObjectsMediumSolve

Inheritance

  1. 188.Python Program to Make Animal Classes Using InheritanceMediumSolve
  2. 189.Python Program to Build a Bank with Savings AccountsHardSolve

What's next after this course?

This is the last chapter of LivePython. Well done! You've gone from your first print() to your own classes. Here are some good next steps:

  • Build small projects. A to-do list saved in a file, a quiz game, a budget tracker. Projects turn what you know into real skill.
  • Practice every day. Come back to these questions, and try to solve them a second way.
  • Explore the standard library. Python comes with many more modules, like the ones you met in chapter 18.
  • Pick a direction. Websites, data and charts, games or automation: each one has its own Python tools to learn next.

Classes bundle data and actions into objects you design. You now know the core of Python: keep building, one small program at a time.