Basic Exercise on Constructor


  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 )
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com







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 Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer