Write a python program to Constructor Initialization


The program defines a Student class and creates three instances of the class, each representing a different student. It then displays the details of each student using the display_details method. Here's a breakdown of the code:

  • class Student: This class defines a student with attributes such as roll_number, name, and percent (percentage).
  • __init__(self, roll_number, name, percent) : The constructor initializes a Student object with the provided roll_number, name, and percent.
  • display_details(self): This method is used to print the details of a student, including their roll number, name, and percentage.
  • Three instances of the Student class (s1, s2, and s3) are created and initialized with different data for each student.
  • The code then displays the details of each student by calling the display_details method for each instance.

Here's what the code does:

  • It creates three student objects with different details.
  • It prints the details of each student using the display_details method, showing their roll number, name, and percentage.

Source Code

class Student:
    def __init__(self, roll_number, name, percent):
        self.roll_number = roll_number
        self.name = name
        self.percent = percent
 
    def display_details(self):
        print("Roll Number :", self.roll_number)
        print("Name :", self.name)
        print("Percentage :", self.percent)
 
# Creating objects of the Student class with constructor initialization
s1 = Student(501, "Kim", 85.5)
s2 = Student(502, "Bob", 78.0)
s3 = Student(503, "Tin", 90.3)
 
print("Student 1 Details :")
s1.display_details()
 
print("\nStudent 2 Details :")
s2.display_details()
 
print("\nStudent 3 Details :")
s3.display_details()

Output

Student 1 Details :
Roll Number : 501
Name : Kim
Percentage : 85.5

Student 2 Details :
Roll Number : 502
Name : Bob
Percentage : 78.0

Student 3 Details :
Roll Number : 503
Name : Tin
Percentage : 90.3

Example Programs