✦ Python Intermediate & OOP Intermediate Python

L2 · Concept 02 · OOP Fundamentals

Build a book.
Create the library.

Mini Library Manager now gives every book a clear title and author when it is created.

Learning goalDefine a Book class, use __init__ and self, and create independent book objects.
01 Learn02 Try03 Predict

Learning as a guest

Learning as a guest? Save your progress!

Create a free account to learn with Sloby AI, check your understanding, and track your progress.

The entire platform is free throughout 2026. Foundations stays free forever.

Give every book the same starting shape.

The previous lesson introduced a class as a shared model. Now Book can create ready-to-use book objects. __init__ runs when Python creates an object, and self means the particular object being prepared.

class Book:
    def __init__(self, title, author):
        self.title = title
        self.author = author

book_one = Book("Dune", "Frank Herbert")
print(book_one.title)

Book(...) calls __init__. The values become attributes on this one object: self.title and self.author. The dot reads an attribute from a particular instance.

Dune

Every call to Book(...) creates a new object. The same class definition is shared, but each instance keeps separate attribute values.

↳

Read self as “this book”: Inside __init__, self refers to the object Python is currently creating. You write self.title so each Book receives its own title.

Two books, separate data
class Book:
    def __init__(self, title, author):
        self.title = title
        self.author = author

book_one = Book("Dune", "Frank Herbert")
book_two = Book("The Hobbit", "J. R. R. Tolkien")
book_one.title = "Dune Messiah"

print(book_one.title)
print(book_two.title)

Changing book_one.title changes only book_one. book_two still stores The Hobbit.

Class versus object
Book                  # the class blueprint
book_one = Book(...)  # one object, or instance

Use the class name to create objects. Use an object variable and a dot to access its attributes.

Create one library book

  1. 1. Define the classclass Book names the shared blueprint.
  2. 2. Initialize data__init__ receives title and author while the object is created.
  3. 3. Store attributesself.title and self.author belong to that one object.
  4. 4. Create another instanceAnother Book call creates separate data for another library book.

Common mistakes

  • Missing selfInstance methods need self first so Python knows which object receives the data.
  • Using title instead of self.titleA parameter is temporary; self.title saves the value as an object attribute.
  • Expecting shared instance dataChanging one Book instance does not update another instance.
  • Forgetting parenthesesBook(...) creates an object; Book by itself names the class.

Python Playground

Loading Python runtime…

Change a title or author, then create another Book instance for the library.

library_books.pyPython
OutputWaiting for Python
The browser runtime is loading. You can still edit the example code.

Pyodide runs Python in a browser Web Worker. It is stopped and restarted if code keeps running. Browser execution is not a complete security boundary; do not enter secrets or rely on it for untrusted code isolation.

What will this program print?

Follow the change to book_one only. Each Book instance has separate attributes.

class Book:
    def __init__(self, title, author):
        self.title = title
        self.author = author

book_one = Book("Dune", "Frank Herbert")
book_two = Book("The Hobbit", "J. R. R. Tolkien")
book_one.title = "Dune Messiah"

print(book_one.title)
print(book_two.title)
Choose the output

Practice independently

Mini Challenge — Register a book

Add a new book to the library by using the class constructor and its attributes.

Your goalCreate a Book for a new title and author, then print a sentence using its attributes.

register_book_challenge.py

Optional hints
  • Call Book with two arguments: a title and an author.
  • Read the saved values with new_book.title and new_book.author.
Ask Sloby for a hint
Sloby, the Python AI Tutor mascot

Your learning mentor

Sign in to ask Sloby

Create a free account or sign in to get AI guidance while you learn.

Sign in to check your understanding

Create a free account or sign in to receive AI feedback and save your learning progress.

Python AI Tutor · Learn by understanding, not just by running code.