Editor

Python Program to Store Student Records Using a Class

Beginner · Classes and objects

This program stores student records as objects of a Student class. Each student has a roll number, a name and marks in three subjects, and methods that work out the total, the percentage and the grade. The objects are kept in a list, so the program can print a report and find the topper.

Program

student_class.py
class Student:
def __init__(self, roll, name, marks):
self.roll = roll
self.name = name
self.marks = marks # a list of marks out of 100
def total(self):
return sum(self.marks)
def percentage(self):
return self.total() / len(self.marks)
def grade(self):
if self.percentage() >= 90:
return "A"
elif self.percentage() >= 75:
return "B"
return "C"
students = [
Student(1, "Aarav", [88, 92, 79]),
Student(2, "Diya", [95, 98, 91]),
Student(3, "Kabir", [62, 70, 58]),
]
# or: read each record with input() and append a Student object to the list
print("Roll Name Total Percent Grade")
topper = students[0]
for s in students:
print(f"{s.roll:<5} {s.name:<7} {s.total():<6} {s.percentage():<8.1f} {s.grade()}")
if s.total() > topper.total():
topper = s
print("Topper:", topper.name, "with", topper.total(), "marks")
Output
Roll  Name    Total  Percent  Grade
1     Aarav   259    86.3     B
2     Diya    284    94.7     A
3     Kabir   190    63.3     C
Topper: Diya with 284 marks

How it works

  1. Each Student object holds one record: a roll number, a name and a list of marks.
  2. total() adds the marks with sum(), and percentage() reuses total() and divides by the number of subjects.
  3. grade() turns the percentage into a letter grade with an if-elif chain.
  4. In the f-string, :<7 pads a value to 7 characters so the columns line up, and .1f shows one decimal place.
  5. The topper is found by keeping the student with the highest total seen so far, which is Diya with 284.

Another way to write it

Searching records by roll number

Searching records by roll number
class Student:
def __init__(self, roll, name, city):
self.roll = roll
self.name = name
self.city = city
def __str__(self):
return f"Roll {self.roll}: {self.name} from {self.city}"
records = {}
for s in [Student(101, "Aarav", "Pune"), Student(102, "Diya", "Chennai"), Student(103, "Kabir", "Jaipur")]:
records[s.roll] = s # roll number -> Student object
for roll in [102, 110]: # or: roll = int(input("Roll number to search: "))
if roll in records:
print(records[roll])
else:
print("No student with roll number", roll)
Output
Roll 102: Diya from Chennai
No student with roll number 110

Keeping the objects in a dictionary keyed by roll number finds a record in one step, and __str__ decides how a student is printed.

Try it yourself

Add a fourth student and a method has_passed() that returns True when every mark is at least 33.

Edit the program above and press Run, or open it in the full editor to save it and share it. To watch it run line by line, open it in the visualizer.

All Python programs · CBSE class 12 practical file · Python exercises