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

python) Declare a function inside a class

by kiseno 2024. 10. 27.
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.korean + self.math + self.english + self.science)/4
    def get_to_string(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(student.get_to_string())

This code snippet defines a `Student` class in Python, along with a list of `Student` instances, and then prints a summary of each student's name, total score, and average score. The `Student` class has methods to calculate the total and average scores and a method to generate a formatted string for printing. Here's a closer look at how it works:

### `Student` Class

- **`__init__` Method**: Initializes a new `Student` instance with name and scores in Korean, math, English, and science.

- **`get_sum` Method**: Returns the total score by summing the individual scores in all subjects.

- **`get_average` Method**: Calculates the average score by dividing the total score by 4 (the number of subjects).

- **`get_to_string` Method**: Returns a formatted string containing the student's name, total score, and average score, separated by tabs. This method effectively packages the student's information in a format suitable for printing.

### Processing and Printing the Student Information

- The `students` list contains several `Student` instances, each representing a student with their name and scores in different subjects.

- The loop iterates over each `Student` instance in the `students` list, calling the `get_to_string` method of each instance to retrieve a formatted string. This string is then printed, resulting in a tabulated output of the students' names, total scores, and average scores.

### Output

The output of this code will be a header row followed by one row for each student, showing their name, total score, and average score, all separated by tabs. For example:

```
name    sum average
윤인성   368 92.0
연하진   384 96.0
구지연   373 93.25
나선주   325 81.25
윤아린   355 88.75
```

This tabulated format makes it easy to read the students' performance at a glance. The implementation showcases basic class definition, instance method creation, and iteration over a list of class instances in Python, demonstrating how object-oriented programming principles can be applied to organize and process data effectively.

728x90
반응형
LIST

'개념 > 혼자 공부하는 파이썬' 카테고리의 다른 글

python) Overriding  (1) 2024.10.29
python) size comparison function  (2) 2024.10.28
python) class parameter  (0) 2024.10.26
python) class function  (1) 2024.10.25
python) function decorator  (2) 2024.10.24