Write a python program to Create Student Class
This Python program defines a Student class with attributes and methods to set and display student data. Here's an explanation of the program:
- class Student: This is the main class representing a student. It has the following attributes and methods:
- Class attributes:
- id: To store the student's ID (initialized to 0 by default).
- name: To store the student's name (initialized as an empty string by default).
- gender: To store the student's gender (initialized as an empty string by default).
- total: To store the student's total marks (initialized as an empty string by default).
- per: To store the student's percentage (initialized to 0 by default).
- setData(self, id, name, gender, total, per): This method is used to set the data for a student. It takes the student's ID, name, gender, total marks, and percentage as parameters and assigns them to the corresponding attributes of the instance.
- showData(self): This method is used to display the student's data. It prints the ID, name, gender, total marks, and percentage of the student.
- An instance of the Student class is created and named s.
- The setData method is called on the s instance to set the data for the student. In this case, the student's ID is set to 1, the name is set to "Sam Kumar," the gender is set to "Male," the total marks are set to 422, and the percentage is set to 84.44.
- The showData method is called on the s instance to print the student's data.
Source Code
class Student():
id = 0
name = ""
gender = ""
total = ""
per = 0
def setData(self,id,name,gender,total,per): # function to set data
self.id = id
self.name = name
self.gender = gender
self.total = total
self.per = per
def showData(self): # function to get/print data
print("Id :",self.id)
print("Name :", self.name)
print("Gender :", self.gender)
print("Total :", self.total)
print("Percentage :", self.per)
s = Student()
s.setData(1,'Sam Kumar','Male',422,84.44)
s.showData()
Output
Id : 1
Name : Sam Kumar
Gender : Male
Total : 422
Percentage : 84.44