Home »
Python »
Python Programs
Program for students marks list using class in Python
Python | Students Marks List: Here, we are going to learn how to implement a Python program to input and manage students marks list using class and object approach?
Submitted by Anuj Singh, on May 06, 2020
Problem statement
All the teachers of all the classes in the school requested the school's Principal to digitalize the student marks record system. Traditionally they are using pen paper for writing the marks of students and make a paper list, which is prone to get lost and there are chances of error also. Listening to the student's perspective the principal assigned its Computer Science Teacher to implement a program for making the list which contains roll number, name, and marks of the students.
The CS teacher decided to use Python - Class for data abstraction and implemented in the this way.
Note: There are many other ways to achieve our goal to make a list of students along with their marks, but the key idea of this particular code is to implement Class and how to use it?
Python program for students marks list using class
# Definig a class student, which contain
# name and Roll number and marks of the student
class Student(object):
def __init__(self, name, roll, marks):
self.name = name
self.roll = roll
self.marks = marks
def getmarks(self):
return self.marks
def getroll(self):
return self.roll
def __str__(self):
return self.name + ' : ' + str(self.getroll()) +' ::'+ str(self.getmarks())
# Defining a function for building a Record
# which generates list of all the students
def Markss(rec, name, roll, marks):
rec.append(Student(name, roll, marks))
return rec
# Main Code
Record = []
x = 'y'
while x == 'y':
name = input('Enter the name of the student: ')
height = input('Enter the roll number: ')
roll = input('Marks: ')
Record = Markss(Record, name, roll, height)
x = input('another student? y/n: ')
# Printing the list of student
n = 1
for el in Record:
print(n,'. ', el)
n = n + 1
Output
Enter the name of the student: Prem
Enter the roll number: 101
Marks: 200
another student? y/n: y
Enter the name of the student: Shivang
Enter the roll number: 102
Marks: 250
another student? y/n: y
Enter the name of the student: Radib
Enter the roll number: 103
Marks: 230
another student? y/n: n
1 . Prem : 200 ::101
2 . Shivang : 250 ::102
3 . Radib : 230 ::103
Python class & object programs »