Advertisement
ZEdKasat

Classes Examples

Dec 23rd, 2021 (edited)
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.48 KB | None | 0 0
  1. ## Class Excercise
  2.  
  3. ##1
  4. class Student():
  5.  
  6.     def __init__(self, name, age, list_of_marks):
  7.         self.name = name
  8.         self.age = age
  9.         # list_of_subjects = ["english", "science", "math"] ## not really required because it is redundant anyways but you can use it because it is in the question.
  10.         self.marks = {
  11.             "english": list_of_marks[0],
  12.             "science": list_of_marks[1],
  13.             "maths": list_of_marks[2],
  14.         }
  15.  
  16. b = Student("Bruce", 24, [92, 95, 99])
  17. a = Student("Alfred", 42, [29, 59, 88])
  18.  
  19. # print(b.marks["maths"])
  20. # print(a.marks["maths"])
  21.  
  22.  
  23. ##2
  24. class Student():
  25.  
  26.     def __init__(self, name, age, list_of_marks):
  27.         self.name = name
  28.         self.age = age
  29.         self.marks = {
  30.             "english": list_of_marks[0],
  31.             "science": list_of_marks[1],
  32.             "maths": list_of_marks[2],
  33.         }
  34.    
  35.     def get_all_subjects(self):
  36.         return list(self.marks.keys()) # because all the keys in the dictionary are basically subjects
  37.    
  38.     def check_subject(self, subject):
  39.         return subject in self.marks.keys()
  40.  
  41.         ### statement above is as good as statements below
  42.         # if subject in self.marks.keys:
  43.         #     return True
  44.         # else:
  45.         #     return False
  46.  
  47.     def getAverage(self):
  48.         return sum(self.marks.values())/len(self.marks.values())
  49.  
  50.  
  51. b = Student("Bruce", 24, [92, 95, 99])
  52. a = Student("Alfred", 42, [29, 59, 88])
  53.  
  54. print(b.getAverage())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement