📖 Scenario: You are creating a simple program to keep track of books in a library. Each book has a title, an author, and a year it was published.
🎯 Goal: Build a program that models books as objects, stores them in a list, and prints details of each book.
📋 What You'll Learn
Create a class called Book with attributes title, author, and year
Create a list called library to hold Book objects
Add three Book objects to the library list with exact details
Use a for loop to print the details of each book in the format: Title by Author (Year)
💡 Why This Matters
🌍 Real World
Modeling real-world items like books helps organize and manage data in software such as library systems or inventory apps.
💼 Career
Understanding how to create and use classes is essential for software development jobs that involve object-oriented programming and data modeling.
Progress0 / 4 steps
1
Create the Book class
Create a class called Book with an __init__ method that takes title, author, and year as parameters and assigns them to instance variables with the same names.
Python
Hint
Use class Book: to start the class. Inside, define def __init__(self, title, author, year): and assign the parameters to self.title, self.author, and self.year.
2
Create the library list and add books
Create a list called library. Add three Book objects to it with these exact details: "The Hobbit", "J.R.R. Tolkien", 1937, "1984", "George Orwell", 1949, and "To Kill a Mockingbird", "Harper Lee", 1960.
Python
Hint
Create a list named library and add three Book objects using the exact titles, authors, and years given.
3
Write a loop to print book details
Use a for loop with variables book to iterate over the library list. Inside the loop, create a string in the format Title by Author (Year) using the book object's attributes.
Python
Hint
Use for book in library: to loop. Inside, use an f-string to format the book's title, author, and year.
4
Print the book details
Inside the for loop, add a print statement to display the details string for each book.
Python
Hint
Use print(details) inside the loop to show each book's information.
Practice
(1/5)
1. Which of the following best describes an object in Python when modeling real-world things?
easy
A. An object is just a list of numbers used for calculations.
B. An object is a special keyword used to start a program.
C. An object is a type of function that runs automatically.
D. An object is a combination of data (attributes) and actions (methods) representing something real.
Solution
Step 1: Understand what an object represents
An object models real-world things by holding data and actions together.
Step 2: Compare options with this understanding
Only An object is a combination of data (attributes) and actions (methods) representing something real. correctly describes an object as data plus actions representing something real.
Final Answer:
An object is a combination of data (attributes) and actions (methods) representing something real. -> Option D
Quick Check:
Object = Data + Actions [OK]
Hint: Objects combine data and actions like real things [OK]
Common Mistakes:
Thinking objects are just lists or numbers
Confusing objects with functions
Believing objects are keywords
2. Which of the following is the correct way to define a simple class named Car in Python?
easy
A. class Car()
B. class Car:
pass
C. def Car:
pass
D. Car = class()
Solution
Step 1: Recall Python class syntax
Classes start with the keyword class, followed by the class name and a colon.
Step 2: Check each option
class Car:
pass correctly uses class Car: and a body with pass. Others have syntax errors.
Final Answer:
class Car:\n pass -> Option B
Quick Check:
class keyword + name + colon = correct class [OK]
Hint: Use 'class ClassName:' to define a class [OK]
Common Mistakes:
Using def instead of class
Missing colon after class name
Trying to assign class to a variable
3. What will be the output of this code?
class Dog:
def __init__(self, name):
self.name = name
def bark(self):
return f"{self.name} says Woof!"
my_dog = Dog('Buddy')
print(my_dog.bark())
medium
A. my_dog says Woof!
B. Woof!
C. Buddy says Woof!
D. Error: missing self parameter
Solution
Step 1: Understand the class and method
The Dog class stores the dog's name and the bark method returns a string with the dog's name.
4. Find the error in this class definition and choose the correct fix:
class Book:
def __init__(title, author):
self.title = title
self.author = author
medium
A. Add 'self' as the first parameter in __init__ method.
B. Change __init__ to init without underscores.
C. Remove self from inside the method.
D. Rename class to book (lowercase).
Solution
Step 1: Identify the __init__ method parameters
The first parameter of any instance method must be self to refer to the object.
Step 2: Check the given code
The __init__ method lacks self as the first parameter, causing an error when assigning attributes.
Final Answer:
Add 'self' as the first parameter in __init__ method. -> Option A
Quick Check:
Instance methods need self first [OK]
Hint: Always put self as first method parameter [OK]
Common Mistakes:
Forgetting self in method parameters
Changing __init__ name incorrectly
Ignoring case sensitivity in class names
5. You want to model a Library that holds many Book objects. Which design best uses classes to represent this real-world situation?
hard
A. Create a Book class with title and author, and a Library class with a list of Book objects as an attribute.
B. Create only a Library class with title and author attributes.
C. Create a Book class with a list of libraries it belongs to, no Library class.
D. Use a single class named BookLibrary with no separate classes.
Solution
Step 1: Understand the real-world relationship
A library contains many books, so it makes sense to have separate classes for each.
Step 2: Check which design models this well
Create a Book class with title and author, and a Library class with a list of Book objects as an attribute. uses a Book class for individual books and a Library class holding a list of books, matching the real-world model.
Final Answer:
Create a Book class with title and author, and a Library class with a list of Book objects as an attribute. -> Option A
Quick Check:
Separate classes + composition = best model [OK]
Hint: Use separate classes and lists to model collections [OK]
Common Mistakes:
Combining unrelated data in one class
Ignoring relationships between objects
Not using lists to hold multiple objects
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.