Unit: Unit 7: Introduction to Classes • Interactive Practice
Interactive Python programming tutorial and practice in your browser.
# Define a Book class with title, author, and page count
class Book:
def __init__(self, title, author, pages):
# Bind parameters to instance attributes using self
self.title = title
self.author = author
self.pages = pages
# Instantiate two distinct books
book1 = Book("1984", "George Orwell", 328)
book2 = Book("To Kill a Mockingbird", "Harper Lee", 281)
print(f"Book 1: '{book1.title}' by {book1.author} ({book1.pages} pages)")
print(f"Book 2: '{book2.title}' by {book2.author} ({book2.pages} pages)")