Extended Student class in Python
class Student:
def __init__(self, first, last, courses=None):
self.first_name = first
self.last_name = last
if courses == None:
self.courses = []
else:
self.courses = courses
def add_course(self, course):
if course not in self.courses:
self.courses.append(course)
else:
print(f"{self.first_name} is already \
enrolled in the {course} course")
def remove_course(self, course):
if course in self.courses:
self.courses.remove(course)
else:
print(f"{course} not found")
courses1 = ['python', 'java', 'javascript']
courses2 = ['java', 'rails', 'c']
dmitri = Student("Dmitri", "Telinov", courses1)
john = Student("John", "Doe", courses2)
print(dmitri.first_name, dmitri.last_name, dmitri.courses)
dmitri.add_course("rails")
dmitri.add_course("java")
print(dmitri.first_name, dmitri.last_name, dmitri.courses)
print(john.first_name, john.last_name, john.courses)
john.remove_course("c")
john.remove_course("c")
john.remove_course("python")
print(john.first_name, john.last_name, john.courses)
Output:
Dmitri Telinov ['python', 'java', 'javascript']
Dmitri is already enrolled in the java course
Dmitri Telinov ['python', 'java', 'javascript', 'rails']
John Doe ['java', 'rails', 'c']
c not found
python not found
John Doe ['java', 'rails']