Welcome to today's lesson! We'll be exploring a practical application of Python sets by managing student enrollments for various courses. Imagine you're running an online course platform and need to handle enrollments, checks, and listings of students in different courses. Sets would be perfect for this kind of problem since they don’t allow duplicates, ensuring that a student can't enroll in the same course more than once!
By the end of this session, you'll be well-versed in using sets for such tasks. Let’s dive in!
Here are the methods we need to implement in our enrollment system:
enroll(self, student: str, course: str) -> None
: This method adds a student to a course. If the student is already enrolled, it does nothing.unenroll(self, student: str, course: str) -> bool
: This method removes a student from a course. It returns True
if the student was enrolled and has now been removed. Otherwise, it returns False
. If after unenrolling the student, the course becomes empty (no one is involved there), remove the course as well.is_enrolled(self, student: str, course: str) -> bool
: This method checks if a student is enrolled in a course. It returns True
if the student is enrolled and False
otherwise.list_students(self, course: str) -> list[str]
: This method returns a list of all students enrolled in a given course. If no students are enrolled, it returns an empty list.Let's look at how to implement each of these methods step-by-step.
We'll start by defining our class and then add each method one by one.
First, we define our EnrollmentSystem
class:
Python1class EnrollmentSystem: 2 def __init__(self): 3 self.enrollments = {}
This code initializes an EnrollmentSystem
class with a dictionary named enrollments
that maps courses to sets of students.
Next, we implement the enroll
method:
Python1class EnrollmentSystem: 2 def __init__(self): 3 self.enrollments = {} 4 5 def enroll(self, student: str, course: str) -> None: 6 if course not in self.enrollments: 7 self.enrollments[course] = set() 8 self.enrollments[course].add(student) 9 10# Example usage: 11es = EnrollmentSystem() 12es.enroll("Alice", "Math101") 13print(es.enrollments) # Output: {'Math101': {'Alice'}}
Here, the enroll
method checks if the course
exists in the enrollments
dictionary. If it doesn't, it initializes a new set for that course. Then, it adds the student
to the set of students for that course.
Let's move on to the unenroll
method:
Python1class EnrollmentSystem: 2 def __init__(self): 3 self.enrollments = {} 4 5 def enroll(self, student: str, course: str) -> None: 6 if course not in self.enrollments: 7 self.enrollments[course] = set() 8 self.enrollments[course].add(student) 9 10 def unenroll(self, student: str, course: str) -> bool: 11 if course in self.enrollments and student in self.enrollments[course]: 12 self.enrollments[course].remove(student) 13 if not self.enrollments[course]: # If the set is empty, remove the course 14 del self.enrollments[course] 15 return True 16 return False 17 18# Example usage: 19es = EnrollmentSystem() 20es.enroll("Alice", "Math101") 21es.enroll("Bob", "Math101") 22print(es.enrollments) # Output: {'Math101': set('Alice', 'Bob')} 23es.unenroll("Alice", "Math101") 24print(es.enrollments) # Output: {'Math101': set('Bob')} 25es.unenroll("Bob", "Math101") 26print(es.enrollments) # Output: {}
This method first checks whether the course
and student
exist in the enrollments. If they do, it removes the student from the course. If the course set becomes empty after removal, it deletes the course from the dictionary. The method returns True
if the student was successfully unenrolled and False
otherwise.
Next, let's implement the is_enrolled
method:
Python1class EnrollmentSystem: 2 def __init__(self): 3 self.enrollments = {} 4 5 def enroll(self, student: str, course: str) -> None: 6 if course not in self.enrollments: 7 self.enrollments[course] = set() 8 self.enrollments[course].add(student) 9 10 def unenroll(self, student: str, course: str) -> bool: 11 if course in self.enrollments and student in self.enrollments[course]: 12 self.enrollments[course].remove(student) 13 if not self.enrollments[course]: # If the set is empty, remove the course 14 del self.enrollments[course] 15 return True 16 return False 17 18 def is_enrolled(self, student: str, course: str) -> bool: 19 return course in self.enrollments and student in self.enrollments[course] 20 21# Example usage: 22es = EnrollmentSystem() 23es.enroll("Alice", "Math101") 24print(es.is_enrolled("Alice", "Math101")) # Output: True 25print(es.is_enrolled("Bob", "Math101")) # Output: False
This method checks whether both the course and the student exist in the enrollments dictionary, returning True
if they do and False
otherwise.
Finally, let's implement the list_students
method:
Python1class EnrollmentSystem: 2 def __init__(self): 3 self.enrollments = {} 4 5 def enroll(self, student: str, course: str) -> None: 6 if course not in self.enrollments: 7 self.enrollments[course] = set() 8 self.enrollments[course].add(student) 9 10 def unenroll(self, student: str, course: str) -> bool: 11 if course in self.enrollments and student in self.enrollments[course]: 12 self.enrollments[course].remove(student) 13 if not self.enrollments[course]: # If the set is empty, remove the course 14 del self.enrollments[course] 15 return True 16 return False 17 18 def is_enrolled(self, student: str, course: str) -> bool: 19 return course in self.enrollments and student in self.enrollments[course] 20 21 def list_students(self, course: str) -> list[str]: 22 if course in self.enrollments: 23 return list(self.enrollments[course]) 24 return [] 25 26# Example usage: 27es = EnrollmentSystem() 28es.enroll("Alice", "Math101") 29es.enroll("Bob", "Math101") 30print(es.list_students("Math101")) # Output: ['Alice', 'Bob'] 31print(es.list_students("Physics101")) # Output: []
This method returns a list of students enrolled in the given course. If the course is not in the enrollments
dictionary, it returns an empty list.
In today's lesson, we learned how to manage student enrollments using Python sets. We implemented methods to enroll and unenroll students, check enrollments, and list students in a course. This task provided a practical way to reinforce your understanding of sets in Python.
I encourage you to move on to the practice to undertake similar challenges to deepen your understanding. Keep experimenting and honing your skills. Happy coding!