0
0
Pythonprogramming~30 mins

Real-world modeling using objects in Python - Mini Project: Build & Apply

Choose your learning style9 modes available
Real-world modeling using objects
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use print(details) inside the loop to show each book's information.