HelpContactFor teachers and institutes
>_StemPy Lab
Mock TestsClasses

>_StemPy Lab

Learn. Solve. Code the solutions. Test yourself.

Live examsFree mock exams, timed and rankedEveryone starts together, and your score and rank come the moment it ends.See the exams →
Live examsInternational live exams are comingTell us which exam you want first. Timed practice papers are open now.See what is coming →

Learn

  • Physics
  • Chemistry
  • Mathematics
  • Biology
  • Python Programming
  • Statistics & Data Science
  • Computer Science
  • Space Science and Astronomy
  • Earth and Environmental Science
  • Classes
  • Groups

Practice

  • Problems
  • Quizzes
  • Mock Tests
  • Prepare for an Exam
  • Question Sets
  • Exams
  • Certificates
  • Python Labs
  • AI Practice Generator
  • Student-Created Tests

Community

  • Discussions
  • Resources
  • Notes
  • Glossary
  • Blog

Account

  • Log in
© 2026 senLearn. All rights reserved.HelpFor SchoolsAboutContactPrivacy PolicyTerms of UseRefund Policy
  1. Python Programming
  2. /Python Going Further
  3. /Classes and Objects

Classes and Objects

Python Programming · High School (11–12) · 2 courses, 16 worked problems, 52 practice questions

A class is a blueprint, and an object is a thing made from it. A class Ball might store a mass and a speed, and have a method that finds the kinetic energy. In Python you write class Ball: and an __init__ method that sets up each new object. Inside the class, self stands for the object being used. Attributes hold the data, and methods are functions that belong to the class. Classes keep related data and actions together, so a large program is easier to read and change. Games, simulations and many libraries are built this way.

With practice you will be able to

  • Write a class with an __init__ method and attributes
  • Add a method that uses self to work on an object's data
  • Create several objects from one class and compare them

On the syllabus of

  • Python Certification
  • IB
  • A-Level

Lessons

  • Inheritance and Special MethodsIs a car a kind of engine, or does it have one? How does print show your own object?High School (11–12)
  • Classes and ObjectsIs a number in Python an object too? How can a bank account refuse a bad withdrawal?High School (11–12)

Practice questions

Choose an answer, then check it. Each one shows a full explanation.

A shop keeps its tax rate in the class body, and a method adds that tax to a price. class Shop: tax = 5 def total(self, price): return price + tax s = Shop() print(s.tax, Shop.tax) print(s.total(100)) Which of these describes the run of this program?

A class defines __eq__, and two objects with the same value are made. class Coin: def __init__(self, value): self.value = value def __eq__(self, other): return self.value == other.value a = Coin(5) b = Coin(5) Which of these expressions are True? Choose every correct option.

One or more options are correct. Tick all the correct ones: a wrong option costs marks, and a part-right answer earns part of the marks.

A parent and a child class each have a private class variable with the same name. class Vault: __limit = 3 def limit(self): return self.__limit class Big(Vault): __limit = 9 print(Big().limit(), hasattr(Big, "__limit"), sorted(k for k in vars(Big) if "limit" in k)) What does the program print?

A program reads the name of a method from the keyboard and calls it. The user types last and presses Enter. class Tool: def first(self, s): return s.split()[0] def last(self, s): return s.split()[-1] cmd = input().strip() t = Tool() if hasattr(t, cmd): print(getattr(t, cmd)("big data")) else: print("no " + cmd) What does the program print?

This program should print 3 words. It stops with TypeError instead. class Sentence: def __init__(self, text): self.words = text.split() def count(): return len(self.words) s = Sentence("to be free") print(s.count(), "words") Which one change makes the program print 3 words?

A method of an object is kept in a variable, and called through that variable. class Builder: def __init__(self): self.parts = [] def add(self, s): self.parts.append(s) return len(self.parts) b = Builder() push = b.add push("x") push("y") print(push("z"), "".join(b.parts), Builder().parts) What does the program print?

A class has one class variable, and its constructor makes one instance variable. class Box: size = 1 def __init__(self, item): self.item = item b = Box("pen") Which of these give True? Choose every correct option.

One or more options are correct. Tick all the correct ones: a wrong option costs marks, and a part-right answer earns part of the marks.

The file app.ini holds two lines, mode=dark and port=80, each with a new line at its end. A class reads the file in its constructor. class Config: def __init__(self, name): self.data = {} with open(name) as f: for line in f: key, value = line.strip().split("=") self.data[key] = value def get(self, key, default="?"): return self.data.get(key, default) c = Config("app.ini") print(c.get("mode"), c.get("port") * 2, c.get("user")) What does the program print?

A method of a class is a generator. The object's text is changed after the generator is made, but before it is read. class Poem: def __init__(self, text): self.text = text def lines(self): for n, line in enumerate(self.text.split("/"), 1): yield str(n) + ":" + line.strip() p = Poem("roses / violets /sugar") g = p.lines() p.text = "new" print(list(g), len(list(p.lines()))) What does the program print?

A shop gives each new order the next serial number, kept on the class. class Order: made = 0 def __init__(self): Order.made += 1 self.serial = Order.made first, second, third = Order(), Order(), Order() print(second.serial, first.made, Order.made) What does the program print?

All 52 practice questions on Classes and Objects →

Worked problems

Each one has a hint, the key idea and a full worked solution.

  • Given these classes, say what each line shows.Easy
  • A Dog object has no name. Find the reason and fix the class.Medium
  • Who says what?Easy
  • The loop below works for any animal. Add a class Bird that says Tweet, and a class Parrot built on…Medium
  • For each line of code, name the special method that Python calls…Easy
  • A class Note has an attribute rupees. Write __lt__ and __str__ so that sorted puts a list of notes…Medium
  • For each pair, say whether you would use inheritance (is a) or composition (has a), and why…Easy
  • For each name in the lines below, say whether it is a class, an object, an attribute or a method…Easy
  • A student is a name and a list of marks…Easy
  • This class has two mistakes. Find them, say what error Python gives, and fix them.Easy
  • Write a class Point with the attributes x and y, and a method distance_to(other) that gives the…Medium
  • What does self hold?Medium

All 16 worked problems on Classes and Objects →

Python labs

  • A child class
  • Shapes with areas
  • Show a Point
  • A Vector class
  • A garage of cars
  • Project: a deck of cards

Related topics

  • Python Going Further
  • Inheritance and Special Methods
  • Files and Data Formats
  • Comprehensions, Iterators and Generators
  • Data Structures
  • Testing and Debugging