Python program to search student record stored using Dictionary

Here, we will write a Python program to search for a student's record whose roll number is given by the user and print all the students with score more than 60 using Dictionary and its methods.
By Shivang Yadav Last updated : December 20, 2023

Python Dictionary is a collection that stores all the data in key-value pairs. This data needs to be unique and can be easily accessed.

Problem statement

We need to take data of students from the user (number of students are given by user) and then store this data in a dictionary. After this, we need to take the roll number of the student whose record is to be searched and find the student’s record and print it. After this we need to print records of all students with percentages greater than 60.

Example:

student = {
    "RollNo" = 1232
    "Name" = RamLal
    "mathsMark" = 65
    "physicsMark" = 55
    "chemistryMark" = 87
}

Python Dictionary is useful as it supports many in-built functions to perform different operations on the data stored in it. Here, we need searching and filtering.

Searching for a specific record using the get() method

Using a dictionary we can use the python's inbuilt method get() which will search for a student record with a given roll number in the dictionary.

Printing student records with percentages greater than 60, we will do this by applying a filter method on student’s records.

Python program to search student record stored using Dictionary

# Python program to search student record using dictionary 

# Student class to get student details and print...
class Student:
    def GetStudent(self):
        self.__rollno = input("Enter Roll No:")
        self.__name = input("Enter Name:")
        self.__physicsMarks = int(input("Enter Physics Marks:"))
        self.__chemistyMarks = int(input("Enter Chemistry Marks:"))
        self.__mathMarks = int(input("Enter Maths Marks:"))
        return(self.__rollno)

    def PutStudent(self):
        print(self.__rollno,self.__name,((self.__physicsMarks+self.__chemistyMarks+self.__mathMarks)/3))
    def Search(self,min,max):
        per = (self.__physicsMarks+self.__mathMarks+self.__chemistyMarks)/3
        if(per>=min and per<=max):
            return True
        else:
            return False

# creating a dictionary to store student record 
studentDict = dict()
n = int(input("How Many Students you Want To Input?"))
for i in range(n):
 S = Student()
 rollno = S.GetStudent()
 studentDict.setdefault(rollno,S)

# Searching for student records with roll numbers provided by the user.
rollno = input("Enter Roll Number you Want Search?")
S = studentDict.get(rollno,"Not Found!")
if(isinstance(S,Student)):
    S.PutStudent()
else:
    print(S)

# Printing records of all users with marks greater than 60% 
print("All students who scored more that 60 percentage are : ")
gradeAStudent = list(filter(lambda s:s.Search(60,100),studentDict.values()))
if(len(gradeAStudent) == 0):
    print("Record Not Found!")
else:
    for S in gradeAStudent:
        S.PutStudent()

Output

How Many Students you Want To Input?5
Enter Roll No:32
Enter Name:John
Enter Physics Marks:45
Enter Chemistry Marks:87
Enter Maths Marks:67
Enter Roll No:2
Enter Name:Jane
Enter Physics Marks:43
Enter Chemistry Marks:89
Enter Maths Marks:94
Enter Roll No:5
Enter Name:Nupur
Enter Physics Marks:89
Enter Chemistry Marks:90
Enter Maths Marks:99
Enter Roll No:5 32
Enter Name:john
Enter Physics Marks:45
Enter Chemistry Marks:87
Enter Maths Marks:67
Enter Roll No:108
Enter Name:Ramesh
Enter Physics Marks:43
Enter Chemistry Marks:23
Enter Maths Marks:54
Enter Roll Number you Want Search?5
5 Nupur 92.66666666666667
All students who scored more that 60 percentage are : 
32 John 66.33333333333333
5 Nupur 92.66666666666667
2 Jane 75.33333333333333

Explanation

In the above code, we have created a dictionary studentDict to store students data. And then asked the user to enter the records and store it into studentDict (repeated records were deleted). Then we asked the user to enter the roll number of the student whose record is to be searched which then is extracted from the dictionary using the get() method. After this we have applied a filter to print the list of all students who got marks more than 60%.

In this example, we have used the following Python topics that you should learn:

Python Dictionary Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.