ByteBoard Notes Notes

Create a dictionary of students to store names and marks obtained in 5 subjects

Informatics Practices XI · Experiment 16 of 36 · Python

★ CBSE suggested11-IPPython programs
Expt. No.16
Page No.32

Student marks dictionary

Aim

To write a Python program that stores each student's name with a list of marks in 5 subjects in a dictionary and displays each student's total and percentage.

Concepts used

Algorithm

  1. Input the number of students.
  2. For each student, input the name and 5 marks into a list; store name: list in the dictionary.
  3. For each name, find the total with sum() and the percentage (total ÷ 5).
  4. Display the results.

Program

# Dictionary of students and their marks in 5 subjects result = {}n = int(input("Number of students: "))for i in range(n):    name = input("Name: ")    marks = []    for j in range(5):        marks.append(int(input("  Marks: ")))    result[name] = marks print("Name     Marks                 Total  Percent")for name in result:    total = sum(result[name])    print(name, result[name], total, total / 5)

A marked line continues the line above it. In your file, write it on the same line.

Output

$ python student_marks_dictionary.py
Number of students: 2 Name: Riya Marks: 78 Marks: 85 Marks: 90 Marks: 66 Marks: 72 Name: Sameer Marks: 55 Marks: 61 Marks: 70 Marks: 48 Marks: 80 Name Marks Total Percent Riya [78, 85, 90, 66, 72] 391 78.2 Sameer [55, 61, 70, 48, 80] 314 62.8

✓ Printed by running this program, not typed by hand.

Result

Riya's marks totalled 391 (78.2%) and Sameer's 314 (62.8%). Each value in the dictionary was a list of five marks.

Viva voce: what the examiner may ask

01Can a dictionary value be a list?

Yes. Values can be of any type; keys must be immutable.

02What does sum(result[name]) add?

All the marks in that student's list.

03How do you display only the keys?

result.keys().

Variations for the lab test

  1. Display the student with the highest total.
  2. Display each student's marks in subject 3 only.
  3. Add a sixth subject to every student's list.
! Common mistake Creating marks = [] once outside the loop makes every student share one growing list.
Back to the 11-IP index