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
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
- Each
Studentobject holds one record: arollnumber, anameand a list ofmarks. total()adds the marks withsum(), andpercentage()reusestotal()and divides by the number of subjects.grade()turns the percentage into a letter grade with anif-elifchain.- In the f-string,
:<7pads a value to 7 characters so the columns line up, and.1fshows one decimal place. - 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
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.
Related programs
All Python programs · CBSE class 12 practical file · Python exercises