Create a dictionary of roll number, name and marks and display students who scored above 75
Expt. No.25
Page No.50
Students scoring above 75
Aim
To write a Python program that creates a dictionary with the roll number, name and marks of n students and displays the names of students who scored above 75.
Concepts used
Algorithm
- Input the number of students.
- For each student, store roll number as the key and [name, marks] as the value.
- Go through the keys; if the marks are above 75, display the roll number, name and marks.
Program
# Students with marks above 75, stored in a dictionary n = int(input("Number of students: "))result = {}for i in range(n): roll = int(input("Roll number: ")) name = input("Name : ") marks = float(input("Marks : ")) result[roll] = [name, marks] print("Students with marks above 75:")for roll in result: if result[roll][1] > 75: print(roll, result[roll][0], result[roll][1])
A marked line continues the line above it. In your file, write it on the same line.
Output
$ python students_above_75.py
Number of students: 4
Roll number: 1
Name : Asha
Marks : 82
Roll number: 2
Name : Rohan
Marks : 67
Roll number: 3
Name : Kabir
Marks : 91
Roll number: 4
Name : Diya
Marks : 75
Students with marks above 75:
1 Asha 82.0
3 Kabir 91.0
✓ Printed by running this program, not typed by hand.
Result
Of the four students, Asha (82.0) and Kabir (91.0) were displayed. Diya, with exactly 75, was not, because the condition is "above 75".
Viva voce: what the examiner may ask
01Why is the roll number used as the key?
Keys must be unique, and every student has a different roll number; names may repeat.
02What does result[roll][1] mean?
result[roll] is the list [name, marks]; index 1 of that list is the marks.
03Can a list be used as a dictionary key?
No. Keys must be immutable; a list can be a value but not a key.
Variations for the lab test
- Display the topper's name and marks.
- Display the class average.
- Change the condition to "75 and above" and check Diya appears.
! Common mistake Writing
>= 75 changes the meaning: the question says "above 75", so 75 itself must not be included.