-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path07class.py
More file actions
54 lines (41 loc) · 1.19 KB
/
07class.py
File metadata and controls
54 lines (41 loc) · 1.19 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
class Student:
# Class variable (shared by all objects)
school_name = "ABC School"
# Constructor
def __init__(self, name, marks):
self.name = name
self.marks = marks
self.data = {self.name: self.marks} # store in dictionary
# Instance method
def display(self):
print("Student Data:", self.data)
# Instance method
def update_marks(self, new_marks):
self.marks = new_marks
self.data[self.name] = new_marks
print("Marks updated!")
# Class method
@classmethod
def change_school(cls, new_school):
cls.school_name = new_school
print("School name changed to", cls.school_name)
# Static method
@staticmethod
def is_pass(marks):
return marks >= 40
# Magic method
def __str__(self):
return f"Name: {self.name}, Marks: {self.marks}, School: {self.school_name}"
# Taking input from user
name = input("Enter student name: ")
marks = int(input("Enter marks: "))
# Creating object
s1 = Student(name, marks)
# Using methods
s1.display()
print(s1)
print("Pass Status:", Student.is_pass(marks))
s1.update_marks(85)
s1.display()
Student.change_school("XYZ School")
print(s1)