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

OOP in Python Exercise

The document provides an overview of object-oriented programming (OOP) in Python, focusing on class definitions, attributes, and methods. It includes an example of a Pet class with methods for initializing attributes, adding/removing items from lists, and creating user-friendly string representations. Additionally, it outlines an exercise to create a Student class with specific attributes and methods for managing student information.

Uploaded by

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

OOP in Python Exercise

The document provides an overview of object-oriented programming (OOP) in Python, focusing on class definitions, attributes, and methods. It includes an example of a Pet class with methods for initializing attributes, adding/removing items from lists, and creating user-friendly string representations. Additionally, it outlines an exercise to create a Student class with specific attributes and methods for managing student information.

Uploaded by

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

OOP in Python

Classes are defined in Python by using the class keyword, followed by the name of the
class and a colon. For example:
Class Pet:

The method __init__ is then used to declare the attributes to be used in each instance
of the class. For example:
def __init__(self, type, breed)
self.type = type
self.breed = breed

Attributes can be given


specific values, e.g. To instantiate new objects (create instances of the class
self.animal_class
Pet) the following code is used:
= "Mammal"
Pet1 = Pet("Dog", "Labrador")
…or can be declared as a Pet2 = Pet("Cat", "Ragdoll")
list, e.g. self.features
= ["Legs", "Eyes",
"Ears"] If an attribute has been declared as a list, a new item can be
added using .append(item) where item is the name
An empty list can also be
of the item to be added. E.g.
initialised, e.g.
self.ailments = [ ] self.features.append("Nose")

Similarly, an item can be removed from an attribute list with .remove(item)

E.g. self.features.remove("Nose")

Attributes can be combined (concatenated) and returned for use elsewhere in a program
using a formatted string, or an “f string”. e.g.
return f"{self.type} {self.breed}"

For an easier to read representation of an object, the __str__ method can be used. E.g.
def __str__(self):
return f"Pet(type: {self.type}, breed: {self.breed},
features: {', '.join(self.features)}"

(.join will display each item in the list of features in a user friendly way)
Exercise
Student Management
Objective:

To practice object-oriented programming (OOP) concepts by creating a Python class for managing
student information.

Instructions:

Create a class named Student with the following attributes:

student_id (string): A unique identifier for each student.

first_name (string): The student's first name.

last_name (string): The student's last name.

courses (list of strings): A list of course names that the student is enrolled in.

Implement the following methods in the Student class:

__init__(self, student_id, first_name, last_name): The constructor method that initializes the
student's ID, first name, and last name. The courses attribute should be initialized as an empty list.

add_course(self, course_name): A method to add a course to the student's list of enrolled courses.

remove_course(self, course_name): A method to remove a course from the student's list of enrolled
courses.

get_full_name(self): A method that returns the student's full name in the format "First Name Last
Name".

__str__(self): A method that provides a user-friendly string representation of the student, including
their student ID, full name, and enrolled courses.

Test your Student class by performing the following actions in your Python script:

 Create at least two Student objects with different student IDs, first names, and last names.
 Add and remove courses for each student.
 Display the student details using the __str__ method.

Hints:

Use a class-level variable to generate unique student IDs.

Handle cases where a course is added or removed multiple times.

Ensure that the __str__ method provides clear and readable information about each student.

You might also like