Skip to content

Commit 121398c

Browse files
Added punctuation and completed step 5: list operations
1 parent 431e2e6 commit 121398c

1 file changed

Lines changed: 21 additions & 14 deletions

File tree

students.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,29 @@
1-
# Define the Student class
2-
from enum import unique
3-
4-
1+
# Defined the Student class.
52
class Student:
63
def __init__ (self, name, email, grades):
74
self.name = name
85
self.email = email
96
self.grades = grades
107

118
# Methods for the Student class
12-
# Add a grade to the student's list of grades
9+
# Add a grade to the student's list of grades.
1310
def add_grade(self, grade):
1411
self.grades.append(grade)
1512

16-
# Calculate the average grade for the student
13+
# Calculate the average grade for the student.
1714
def average_grade(self):
1815
if len(self.grades) == 0:
1916
return 0
2017
return sum(self.grades) / len(self.grades)
2118

22-
# Display information about the student
19+
# Display information about the student.
2320
def display_info(self):
2421
print(f"Name: {self.name}")
2522
print(f"Email: {self.email}")
2623
print(f"Grades: {self.grades}")
2724
print(f"Average Grade: {self.average_grade()}")
2825

29-
# Return grades as a tuple and demonstrate immutability using try-except block
26+
# Return grades as a tuple and demonstrate immutability using try-except block.
3027
def grades_tuple(self):
3128
return tuple(self.grades)
3229
try:
@@ -37,33 +34,43 @@ def grades_tuple(self):
3734

3835

3936

40-
# Created student objects with their name, email, and list of initial grades
37+
# Created student objects with their name, email, and list of initial grades.
4138
student1 = Student('John', 'jbschoolname@gmail.com', [85, 90, 78])
4239
student2 = Student('Jane', 'jaschoolname@gmail.com', [92, 88, 87])
4340
student3 = Student('Tim', 'tdscoolname@gmail.com', [90, 95, 100])
4441

45-
# Add a new grade for each student
42+
# Add a new grade for each student.
4643
student1.add_grade(82)
4744
student2.add_grade(95)
4845
student3.add_grade(98)
4946

50-
# Display information about each student
47+
# Display information about each student.
5148
student1.display_info()
5249
student2.display_info()
5350
student3.display_info()
5451

55-
# Create a dictionary to store students by their email for easy access
52+
# Create a dictionary to store students by their email for easy access.
5653
student_dict = {
5754
student1.email: student1,
5855
student2.email: student2,
5956
student3.email: student3
6057
}
6158

62-
# Retrieve a student by their email
59+
# Retrieve a student by their email.
6360
def get_student_by_email(email):
6461
return student_dict.get(email)
6562

6663
# Unique grades for all students
6764
def unique_grades(student1, student2, student3):
6865
unique = {(set(student1.grades)).symmetric_difference(set(student2.grades)).symmetric_difference(set(student3.grades))}
69-
print(unique)
66+
print(unique)
67+
68+
# Removed each students last grade using pop().
69+
student1.grades.pop()
70+
student2.grades.pop()
71+
student3.grades.pop()
72+
73+
# Prints first and last grade for each student and the total number of grades.
74+
for student in [student1, student2, student3]:
75+
print(student.grades[0], student.grades[-1])
76+
print(len(student.grades))

0 commit comments

Comments
 (0)