Practice Python programming with beginner-friendly exercises on classes and objects. Learn to define attributes, methods, and create objects with step-by-step tasks.
Sure! Here’s a beginner-friendly exercise involving classes and basic tasks like defining attributes, methods, and creating objects.
Book
ClassTask: Write a class called Book
that represents a book in a library. The class should have:
title
(string)author
(string)year
(integer)is_checked_out
(boolean, default is False
)display_info()
Prints the book’s title, author, and year.
check_out()
Sets is_checked_out
to True
and prints "Book has been checked out."
If the book is already checked out, print "Book is already checked out."
return_book()
Sets is_checked_out
to False
and prints "Book returned."
If the book wasn’t checked out, print "Book was not checked out."
If you want to check your version, here’s what the output should look like when you run this:
book1 = Book("The Alchemist", "Paulo Coelho", 1988)
book1.display_info()
book1.check_out()
book1.check_out()
book1.return_book()
book1.return_book()
Expected Output:
Title: The Alchemist, Author: Paulo Coelho, Year: 1988
Book has been checked out.
Book is already checked out.
Book returned.
Book was not checked out.
Let me know when you’re ready to see the solution — or I can review your version if you write it!