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): = name

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

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

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

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

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

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

© Let’s Data Science


Unlock AI & Data Science treasures. Log in!