Basic Exercise with Solution on Class Object Method

Tutorial on Class Object & Method
  1. Basic Exercise on Object Oriented Programming
  2. Create one class student_kit
  3. Within the student_kit class create one class attribute principal name ( Mr ABC )
  4. Create one attendance method and take input as number of days.
  5. While creating student take input their name .
  6. 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

  1. What is the name of the method used in student_kit class ?
  2. What is the class attribute used in above code ?
  3. Which is the object created in above code ?
  4. How the method got access to class attributes ?
  5. What is the instance attribute in above code ?
  6. What is the data type of the created object ( kalu) ?

Answers

  1. attendance
  2. principal_name
  3. kalu
  4. self.principal_name
  5. self.present
  6. 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 )
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    15-05-2023

    We greatly appreciate your contribution in sharing your invaluable response.

    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer