본문 바로가기
개념/혼자 공부하는 파이썬

python) Student Class and Its Functionality

by kiseno 2024. 12. 20.
728x90
반응형
SMALL
class Student:
    def __init__(self,name,korean,math,english,science):
        self.name = name
        self.korean = korean
        self.math = math
        self.english = english
        self.science = science

    def get_sum(self):
        return self.korean + self.math + self.english + self.science

    def get_average(self):
        return self.get_sum() / 4

    def __str__(self):
        return "{}\t{}\t{}".format(self.name, self.get_sum(), self.get_average())

students = [
    Student("윤인성", 87,98,88,95),
    Student("연하진",92,98,96,98),
    Student("구지연",98,96,90,89),
    Student("나선주",88,86,64,87),
    Student("윤아린",98,99,78,80),
]

print("name", "sum", "average", sep="\t")
for student in students:
    print(str(student))

### Student Class and Its Functionality

This code defines a `Student` class and demonstrates how to create a list of `Student` objects, compute their total and average scores, and print these details in a tabulated format. Here's a breakdown:

1. **`class Student:`** Defines a class named `Student`.

2. **`def __init__(self,name,korean,math,english,science):`** This is the initializer method for the `Student` class. It initializes a new instance of `Student` with the attributes `name`, `korean`, `math`, `english`, and `science`, which represent the student's name and their scores in Korean, math, English, and science, respectively.

3. **`def get_sum(self):`** A method that calculates the sum of the scores in all subjects for a student and returns it.

4. **`def get_average(self):`** This method calculates the average score by calling the `get_sum()` method and dividing the result by 4 (the number of subjects).

5. **`def __str__(self):`** This special method is overridden to return a string representation of the `Student` object. When `print()` is called on a `Student` instance, it returns a formatted string containing the student's name, the sum of their scores, and their average score, separated by tabs.

6. **`students = [...]`** Initializes a list named `students` with `Student` objects. Each object is initialized with a name and scores in four subjects.

7. **`print("name", "sum", "average", sep="\t")`** Prints the header row for the output table, with the column names "name", "sum", and "average", separated by tabs.

8. **`for student in students:`** Iterates over each `Student` object in the `students` list.

9. **`print(str(student))`** For each `student` in the loop, prints the string representation of the `Student` object, which includes the student's name, the sum of their scores, and their average score, formatted as specified in the `__str__` method.

This code elegantly demonstrates object-oriented programming in Python, particularly how to define a class with methods that operate on its attributes, and how to override the `__str__` method to customize how objects are represented as strings.

728x90
반응형
LIST