Basic Exercise on Constructor

Tutorial on Class Object & Method
  1. Basic Exercise on Object Oriented Programming
  2. Create one class student_kit
  3. Initialize the class by taking name of the student ( constructor ).
  4. Display welcome message once any object is created.

Exercise Solution
class student_kit():
       
    def __init__(self,name):
        self.name=name
        print("Welcome",self.name)
    
kalu=student_kit("Kalu")
Output
Welcome Kalu

Answer these Questions

  1. What is the name of the object ?
  2. Why the welcome message is printed when we have not called any method to do so?
Extend the above code by adding one method which takes one string input ( section ). How the object can access the instance attribute ?
class student_kit():
       
    def __init__(self,name):
        self.name=name
        print("Welcome",self.name)
    
    def section(self,s1):
        self.section_name=s1  #instance attribute
    
    
kalu=student_kit("Kalu")  # object declaration
kalu.section('A')         # using section method
print(kalu.section_name)  # object accessing instance attribute

How the object uses any method or instance attribute

print(kalu.section_name)  # instant attribute
print(kalu.section('A')   # method
Tutorial on Class Object & Method Exercise methods ( basic )
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    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