0% found this document useful (0 votes)
8 views

class Library

The document defines a Library class that represents a library with attributes for its name and a list of books. It includes methods to add, remove, and list books in the library. The class is initialized with a name and an empty book list.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

class Library

The document defines a Library class that represents a library with attributes for its name and a list of books. It includes methods to add, remove, and list books in the library. The class is initialized with a name and an empty book list.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

class Library:

"""
A class to represent a library.

Attributes:
name (str): The name of the library.
books (list): A list of books available in the library.

Methods:
add_book(book): Adds a book to the library's collection.
remove_book(book): Removes a book from the library's collection.
list_books(): Prints a list of all books in the library.
"""

def __init__(self, name):


"""
Initializes the Library class with a name and an empty book list.

Parameters:
name (str): The name of the library.
"""
self.name = name
self.books = []

def add_book(self, book):


"""
Adds a book to the library's collection.
Parameters:
book (str): The title of the book to add.
"""
self.books.append(book)

def remove_book(self, book):


"""
Removes a book from the library's collection.

Parameters:
book (str): The title of the book to remove.

Raises:
ValueError: If the book is not found in the collection.
"""
if book not in self.books:
raise ValueError(f"'{book}' not found in the library.")
self.books.remove(book)

def list_books(self):
"""Prints a list of all books in the library."""
if not self.books:
print("The library is empty.")
else:
print(f"Books in {self.name}: {', '.join(self.books)}")

You might also like