Editor

Python Program to Find Students Who Scored Above 75 Using a Dictionary

Beginner · Dictionaries · CBSE class 11 practical list

This program stores the roll number, name and marks of a few students in a dictionary and prints the names of the students who scored more than 75. Each roll number is a key, and its value is a tuple holding the name and the marks.

Program

students_above_75.py
# Roll number -> (name, marks)
students = {
1: ("Aarav", 82),
2: ("Diya", 68),
3: ("Kabir", 91),
4: ("Meera", 75),
5: ("Rohan", 77),
}
# To type the data in instead, add each student in a loop:
# roll = int(input("Roll number: "))
# name = input("Name: ")
# marks = int(input("Marks: "))
# students[roll] = (name, marks)
print("Students who scored above 75:")
found = False
for roll in students:
name, marks = students[roll]
if marks > 75:
print(f"Roll {roll}: {name} ({marks} marks)")
found = True
if not found:
print("No student scored above 75")
Output
Students who scored above 75:
Roll 1: Aarav (82 marks)
Roll 3: Kabir (91 marks)
Roll 5: Rohan (77 marks)

How it works

  1. Each key of students is a roll number, and each value is a (name, marks) tuple.
  2. Looping over a dictionary gives its keys, so roll takes each roll number in turn.
  3. name, marks = students[roll] unpacks the tuple into two separate variables.
  4. Only marks strictly greater than 75 are printed, so Meera, with exactly 75, is left out.
  5. found remembers whether anyone qualified, so the program can say so when nobody did.

Another way to write it

Using a nested dictionary

Using a nested dictionary
students = {
101: {"name": "Aarav", "marks": 82},
102: {"name": "Diya", "marks": 68},
103: {"name": "Kabir", "marks": 91},
}
toppers = []
for roll, info in students.items():
if info["marks"] > 75:
toppers.append(info["name"])
print("Scored above 75:", toppers)
Output
Scored above 75: ['Aarav', 'Kabir']

Storing each student as a small dictionary names every field, so info["marks"] reads more clearly than students[roll][1].

Try it yourself

Add a sixth student with 76 marks, then change the condition to marks >= 75 so Meera is included too.

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