-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstudents.py
More file actions
99 lines (82 loc) · 3.14 KB
/
Copy pathstudents.py
File metadata and controls
99 lines (82 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# Defined the Student class.
class Student:
def __init__(self, name, email, grades):
self.name = name
self.email = email
self.grades = grades
# Methods for the Student class
# Add a grade to the student's list of grades.
def add_grade(self, grade):
self.grades.append(grade)
# Calculate the average grade for the student.
def average_grade(self):
if len(self.grades) == 0:
return 0
return sum(self.grades) / len(self.grades)
# Display information about the student.
def display_info(self):
print("-----------------------------")
print("Student Information:")
print(f"Name: {self.name}")
print(f"Email: {self.email}")
print(f"Grades: {self.grades}")
print(f"Average Grade: {self.average_grade()}")
print(" ")
# Return grades as a tuple and demonstrate immutability using try-except block.
def grades_tuple(self):
return tuple(self.grades)
def demonstrate_immutability(self):
t = self.grades_tuple()
try:
t[0] = 100
except TypeError as e:
print(f"Error: {e} - Tuples are immutable, cannot modify grades.")
# Created student objects with their name, email, and list of initial grades.
student1 = Student('John', 'jbschoolname@gmail.com', [85, 90, 78])
student2 = Student('Jane', 'jaschoolname@gmail.com', [92, 88, 87])
student3 = Student('Tim', 'tdscoolname@gmail.com', [90, 95, 100])
# Add a new grade for each student.
student1.add_grade(82)
student1.add_grade(85)
student2.add_grade(95)
student2.add_grade(90)
student3.add_grade(98)
student3.add_grade(97)
# Display information about each student.
student1.display_info()
student2.display_info()
student3.display_info()
# Create a dictionary to store students by their email for easy access.
student_dict = {
student1.email: student1,
student2.email: student2,
student3.email: student3
}
# Retrieve a student by their email.
def get_student_by_email(email):
return student_dict.get(email)
# Unique grades for all students.
def unique_grades(student1, student2, student3):
all_students = [student1, student2, student3]
unique = set()
for student in all_students:
for grade in student.grades:
if all(grade not in other.grades for other in all_students if other != student):
unique.add(grade)
print(f"Grades unique to only one student: {unique}")
print(" ")
unique_grades(student1, student2, student3)
# Demonstrated immutability of tuples.
student1.demonstrate_immutability()
student2.demonstrate_immutability()
student3.demonstrate_immutability()
# Removed each students last grade using pop().
student1.grades.pop()
student2.grades.pop()
student3.grades.pop()
# Prints first and last grade for each student and the total number of grades.
for student in [student1, student2, student3]:
print("-----------------------------")
print(f"{student.name}'s First and Last Grades:")
print(student.grades[0], student.grades[-1])
print(f"Total number of grades: {len(student.grades)}")