0% found this document useful (0 votes)
12 views2 pages

Unit 13 OOP - Student & Library

The document outlines two Python classes: Student and Library. The Student class includes attributes for name, age, and grades, along with methods to calculate the average grade and display details. The Library class manages a list of books with methods to add, remove, and display books, including confirmation messages for each action.

Uploaded by

saiyannsic20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

Unit 13 OOP - Student & Library

The document outlines two Python classes: Student and Library. The Student class includes attributes for name, age, and grades, along with methods to calculate the average grade and display details. The Library class manages a list of books with methods to add, remove, and display books, including confirmation messages for each action.

Uploaded by

saiyannsic20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Unit 13: OOP (Object-Oriented Programming) - All

Questions

Q1. Create a class named Student that represents a student with basic
attributes.
a. Attributes and Methods in Class:

Attributes:
name: Stores the student's name.
age: Stores the student's age.
grades: A list of grades.
Methods:
average_grade(): Calculates the average of all grades.
display_details(): Prints the student's name, age, and average grade.

(***) Define an __init__ method that initializes the student's name, age, and a list of grades.

Expected Output:

Student: Keo Vichet, Age: 20


Grades: [85, 90, 78, 92], Average Grade: 86.25

Q2. Write the Library Class


a. Write a Python class called Library that has an attribute books initialized as an empty list in the __init__
method.

b. Add a method called add_book that takes a book_title as input and appends it to the books list. Print a message
confirming the book was added.

c. Add a method called remove_book that takes a book_title as input. If the book exists in the books list, remove it
and print a confirmation message. If the book does not exist, print a message saying the book was not found.

d. Add a method called display_books that prints all the books in the library. If the library is empty, print a message
saying "The library is empty."

Expected Output:

Added 'Harry Potter 1' to the library.


Added 'The Great Gatsby' to the library.
Books in the library:
- Harry Potter 1
- The Great Gatsby

Removed 'Harry Potter 1' from the library.


Books in the library:
- The Great Gatsby

Go to Answer Page

You might also like