Person Class with Student and Teacher Subclasses

Implement a class Person, then create child classes Student and Teacher with additional attributes and methods specific to them. Student should have a method to return grade level, and Teacher should have a method to return the subject they teach.

Example 1:

Input: Student("John", "10th") 
Output: "John is in 10th grade."

Example 2:

Input: Teacher("Emily", "Math") 
Output: "Emily teaches Math."

Define the Student and Teacher classes as child classes of Person and provide unique attributes and methods to each.

class Person:
    def __init__(self, name):
        self.name = name

class Student(Person):
    def __init__(self, name, grade_level):
        super().__init__(name)
        self.grade_level = grade_level

    def info(self):
        return f"{self.name} is in {self.grade_level} grade."

class Teacher(Person):
    def __init__(self, name, subject):
        super().__init__(name)
        self.subject = subject

    def info(self):
        return f"{self.name} teaches {self.subject}."

# Test the classes
student = Student("John", "10th")
print(student.info())  # Output: John is in 10th grade.

teacher = Teacher("Emily", "Math")
print(teacher.info())  # Output: Emily teaches Math.

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!