Basic Exercise with Solution on Class Object Method
Python Class Object Method
Basic Exercise on Object Oriented Programming
Create one class student_kit
Within the student_kit class create one class attribute principal name ( Mr ABC )
Create one attendance method and take input as number of days.
While creating student take input their name .
Create one certificate for each student by taking input of number of days present in class.
Exercise Solution
class student_kit():
principal_name='Mr ABC'
def __init__(self,name):
self.name=name
def attendance(self,days):
self.present=days+10
print("Name : ",self.name)
print("Present :",self.present)
print("Principal : ",self.principal_name)
kalu=student_kit("Kalu")
kalu.attendance(20)
Output
Name : Kalu
Present : 30
Answer these Questions
What is the name of the method used in student_kit class ?
What is the class attribute used in above code ?
Which is the object created in above code ?
How the method got access to class attributes ?
What is the instance attribute in above code ?
What is the data type of the created object ( kalu) ?
Answers
attendance
principal_name
kalu
self.principal_name
self.present
print(type(kalu)) # <class '__main__.student_kit'>
How the object can access the instance attribute ?
print(kalu.present) # 30
« Tutorial on Class Object & Method
Exercise constructor ( basic )»
Author & Instructor at
plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery,
and web development at plus2net. The tutorials focus on clear explanations, working
examples, and code that readers can test and adapt while learning.
← Subscribe to our YouTube Channel here
This article is written by plus2net.com team.
https://www.plus2net.com
plus2net.com
15-05-2023
We greatly appreciate your contribution in sharing your invaluable response.