0
0
Pythonprogramming~30 mins

Why object-oriented programming is used in Python - See It in Action

Choose your learning style9 modes available
Why object-oriented programming is used
📖 Scenario: Imagine you are organizing a small library. You want to keep track of books and their details like title, author, and whether they are borrowed or not.
🎯 Goal: You will create a simple program using object-oriented programming to represent books and check their status.
📋 What You'll Learn
Create a class called Book with attributes for title, author, and borrowed status
Create an instance of Book with specific details
Add a method to check if the book is borrowed
Print the book details and borrowed status
💡 Why This Matters
🌍 Real World
Object-oriented programming helps organize data and behavior together, like keeping track of books in a library system.
💼 Career
Many software jobs use object-oriented programming to build clear and reusable code for applications.
Progress0 / 4 steps
1
Create the Book class
Create a class called Book with an __init__ method that takes title, author, and borrowed as parameters and assigns them to instance variables.
Python
Need a hint?

Use class Book: to start the class. Inside, define __init__ with self and the three parameters. Assign each parameter to self variables.

2
Create a Book instance
Create a variable called my_book and assign it an instance of Book with title 'The Little Prince', author 'Antoine de Saint-Exupéry', and borrowed status False.
Python
Need a hint?

Use the class name Book with parentheses and pass the exact values for title, author, and borrowed status.

3
Add a method to check borrowed status
Inside the Book class, add a method called is_borrowed that returns the value of self.borrowed.
Python
Need a hint?

Define a method with def is_borrowed(self): and return self.borrowed.

4
Print book details and borrowed status
Print the book's title and author using my_book.title and my_book.author. Then print Borrowed: followed by the result of my_book.is_borrowed().
Python
Need a hint?

Use print with f-strings to show the title, author, and borrowed status by calling my_book.is_borrowed().