Learn with Yasir
Share Your Feedback

Python Exercises for Beginners - Classes and Objects


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.


🧠 Exercise: Create a Book Class

Task: Write a class called Book that represents a book in a library. The class should have:

Attributes

  • title (string)
  • author (string)
  • year (integer)
  • is_checked_out (boolean, default is False)

Methods

  1. display_info()
    Prints the book’s title, author, and year.

  2. 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."

  3. 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."


✍️ Try It Yourself First!

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!


🧠 Practice & Progress

Explore More Topics

Python Fundamentals

Flow Control Statements


Python Functions


Fundamentals more ...




🧠 Python Advanced

Object-Oriented Programming in Python (OOP)

More...

🧠 Modules