Python Q2) Create a Student class and initialize it with name and roll number. Make methods to : Display - It should display all informations of the student. setAge - It should assign age to student setMarks - It should assign marks to the student.
class Student:
#method to initialize the object
def __init__(obj, name, rollNum):
obj.name = name
obj.roll = rollNum
obj.age = 0
obj.marks = 0
#method to set the age
def setAge(obj, ag):
obj.age = ag
#method to set the marks
def setMarks(obj, mk):
obj.marks = mk
#method to display the student information
def display(obj):
print("Name: " + obj.name)
print("Roll Number: " + str(obj.roll))
print("Age: " + str(obj.age))
print("Marks: " + str(obj.marks))
#create object
s1 = Student("John", 101)
#method calling
s1.setAge(18)
s1.setMarks(70)
s1.display()
The screenshot of the above source code is given below:
OUTPUT:
Name: John
Roll Number: 101
Age: 18
Marks: 70
Get Answers For Free
Most questions answered within 1 hours.