Create a dictionary with the roll number, name and marks of n students in a class and display the names of students who have marks above 75 in Python, C and Java

“Create a dictionary with the roll number, name and marks of n students in a class and display the names of students who have marks above 75” is a standard Python practical for Class 11 and 12 computer science. It combines three important ideas: a dictionary with a key and a value, a loop that fills it from user input, and a second loop that filters it with a condition. In this guide you will learn how to write the program in Python, with matching versions in C and Java, together with the logic, a dry run, tested output and an interactive tool that shows the dictionary being built and filtered step by step.

★ Student Marks Dictionary

Build a dictionary of roll number, name and marks, then list the students who scored above 75.

Graphical view

Code

Output

What You Will Learn

Problem Statement

Create a dictionary with the roll number, name and marks of n students in a class and display the names of the students who have marks above 75.

The user enters the number of students, n, and then the roll number, name and marks of each student. The program stores every student in a dictionary and finally prints the names of those whose marks are above 75. The interactive tool above accepts 1 to 8 students. Names are one word, and marks are from 0 to 100.

What Is a Dictionary?

A dictionary stores data as key: value pairs. You find a value by asking for its key, just like finding a word’s meaning in a dictionary. A list uses the numbers 0, 1, 2 … as positions, but a dictionary lets you choose meaningful keys, such as a roll number.

ListDictionary
Brackets[ ]{ }
You find an item byits position (index)its key
Examplemarks[0]students[101]
Keys must be unique?Not applicableYes: a repeated key replaces the old value
OrderKeptKept in the order of insertion (Python 3.7 and later)

Three operations are all you need in this program:

  • students = {} creates an empty dictionary.
  • students[roll] = value adds a new entry, or replaces the value if the key already exists.
  • for roll in students: visits every key, one by one, in the order they were added.

How to Design the Dictionary for This Problem

Each student has three facts: a roll number, a name and marks. The roll number is unique for every student, so it is the natural key. The name and marks together form the value, and we keep them in a small inner dictionary. That gives a dictionary of dictionaries:

students = {101: {"name": "Aman", "marks": 82}, 102: {"name": "Riya", "marks": 68}, 103: {"name": "Karan", "marks": 91}, 104: {"name": "Neha", "marks": 75}, 105: {"name": "Sita", "marks": 79}}

The marks of student 103 are then students[103]["marks"], and the name is students[103]["name"].

PartRoleExample
KeyRoll number101
ValueInner dictionary with name and marks{“name”: “Aman”, “marks”: 82}
Inner key “name”The student’s name“Aman”
Inner key “marks”The student’s marks (a number)82

C has no built-in dictionary, so the C program uses three arrays that share the same index: roll[i], name[i] and marks[i]. Java uses two LinkedHashMap objects with the roll number as the key.

The Program

Logic:

  • Read n, the number of students, and create an empty dictionary.
  • Repeat n times: read the roll number, the name and the marks, and store them with students[roll] = {"name": name, "marks": marks}.
  • Print a heading, and set count = 0.
  • Visit every key. If students[roll]["marks"] > 75, print the name and add 1 to count.
  • If count is still 0 at the end, print a message that nobody scored above 75.

The marks are read with int() so that they are compared as numbers. The condition is “above 75”, so a student with exactly 75 marks is not printed.

Python program

n = int(input("Enter number of students: "))
students = {}
for i in range(n):
    roll = int(input("Enter roll number: "))
    name = input("Enter name: ")
    marks = int(input("Enter marks: "))
    students[roll] = {"name": name, "marks": marks}
print("Students with marks above 75:")
count = 0
for roll in students:
    if students[roll]["marks"] > 75:
        print(students[roll]["name"])
        count = count + 1
if count == 0:
    print("No student has marks above 75")
C program
#include <stdio.h>
int main() {
    int n;
    printf("Enter number of students: ");
    scanf("%d", &n);
    int roll[50], marks[50];
    char name[50][20];
    for (int i = 0; i < n; i++) {
        printf("Enter roll number: ");
        scanf("%d", &roll[i]);
        printf("Enter name: ");
        scanf("%s", name[i]);
        printf("Enter marks: ");
        scanf("%d", &marks[i]);
    }
    printf("Students with marks above 75:\n");
    int count = 0;
    for (int i = 0; i < n; i++) {
        if (marks[i] > 75) {
            printf("%s\n", name[i]);
            count++;
        }
    }
    if (count == 0) {
        printf("No student has marks above 75\n");
    }
    return 0;
}
Java program
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Scanner;
public class StudentMarks {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter number of students: ");
        int n = sc.nextInt();
        Map<Integer, String> names = new LinkedHashMap<>();
        Map<Integer, Integer> marks = new LinkedHashMap<>();
        for (int i = 0; i < n; i++) {
            System.out.print("Enter roll number: ");
            int roll = sc.nextInt();
            System.out.print("Enter name: ");
            String name = sc.next();
            System.out.print("Enter marks: ");
            int m = sc.nextInt();
            names.put(roll, name);
            marks.put(roll, m);
        }
        System.out.println("Students with marks above 75:");
        int count = 0;
        for (int roll : names.keySet()) {
            if (marks.get(roll) > 75) {
                System.out.println(names.get(roll));
                count++;
            }
        }
        if (count == 0) {
            System.out.println("No student has marks above 75");
        }
    }
}

Sample output:

Enter number of students: 5
Enter roll number: 101
Enter name: Aman
Enter marks: 82
Enter roll number: 102
Enter name: Riya
Enter marks: 68
Enter roll number: 103
Enter name: Karan
Enter marks: 91
Enter roll number: 104
Enter name: Neha
Enter marks: 75
Enter roll number: 105
Enter name: Sita
Enter marks: 79
Students with marks above 75:
Aman
Karan
Sita

Another run (nobody above 75):

Enter number of students: 3
Enter roll number: 11
Enter name: Raj
Enter marks: 40
Enter roll number: 12
Enter name: Mina
Enter marks: 75
Enter roll number: 13
Enter name: Tara
Enter marks: 60
Students with marks above 75:
No student has marks above 75

To print the roll number, name and marks of these students instead of only the names, change the print line. The rest of the program stays the same.

Python print line

print(roll, students[roll]["name"], students[roll]["marks"])

Sample output of the full record version:

Enter number of students: 5
Enter roll number: 101
Enter name: Aman
Enter marks: 82
Enter roll number: 102
Enter name: Riya
Enter marks: 68
Enter roll number: 103
Enter name: Karan
Enter marks: 91
Enter roll number: 104
Enter name: Neha
Enter marks: 75
Enter roll number: 105
Enter name: Sita
Enter marks: 79
Students with marks above 75:
101 Aman 82
103 Karan 91
105 Sita 79

Dry Run

Take the five students 101 Aman 82, 102 Riya 68, 103 Karan 91, 104 Neha 75 and 105 Sita 79. After the input loop the dictionary holds:

students = {101: {"name": "Aman", "marks": 82}, 102: {"name": "Riya", "marks": 68}, 103: {"name": "Karan", "marks": 91}, 104: {"name": "Neha", "marks": 75}, 105: {"name": "Sita", "marks": 79}}

Now the second loop visits each key:

rollmarksComparisonResultcount
1018282 > 75?yes: print Aman1
1026868 > 75?no1
1039191 > 75?yes: print Karan2
1047575 > 75?no (exactly 75)2
1057979 > 75?yes: print Sita3

The printed names are Aman, Karan and Sita, and count ends at 3. Neha scored exactly 75, which is not above 75, so she is skipped.

Other Ways to Write the Program

Method 2: Looping with items()

The items() method gives the key and the value together, so you do not need to write students[roll] again and again. The code becomes shorter and easier to read.

n = int(input("Enter number of students: "))
students = {}
for i in range(n):
    roll = int(input("Enter roll number: "))
    name = input("Enter name: ")
    marks = int(input("Enter marks: "))
    students[roll] = {"name": name, "marks": marks}
print("Students with marks above 75:")
for roll, info in students.items():
    if info["marks"] > 75:
        print(info["name"])

Sample output:

Enter number of students: 3
Enter roll number: 1
Enter name: Om
Enter marks: 99
Enter roll number: 2
Enter name: Ila
Enter marks: 88
Enter roll number: 3
Enter name: Dev
Enter marks: 76
Students with marks above 75:
Om
Ila
Dev

Method 3: List comprehension

A list comprehension builds the list of names in a single line: “take the name of every student whose marks are above 75”.

n = int(input("Enter number of students: "))
students = {}
for i in range(n):
    roll = int(input("Enter roll number: "))
    name = input("Enter name: ")
    marks = int(input("Enter marks: "))
    students[roll] = {"name": name, "marks": marks}
toppers = [info["name"] for roll, info in students.items() if info["marks"] > 75]
print("Students with marks above 75:", toppers)

Sample output:

Enter number of students: 5
Enter roll number: 101
Enter name: Aman
Enter marks: 82
Enter roll number: 102
Enter name: Riya
Enter marks: 68
Enter roll number: 103
Enter name: Karan
Enter marks: 91
Enter roll number: 104
Enter name: Neha
Enter marks: 75
Enter roll number: 105
Enter name: Sita
Enter marks: 79
Students with marks above 75: ['Aman', 'Karan', 'Sita']

Method 4: Dictionary comprehension

A dictionary comprehension keeps the whole record, not only the name. It builds a new dictionary that contains just the students who passed the condition.

n = int(input("Enter number of students: "))
students = {}
for i in range(n):
    roll = int(input("Enter roll number: "))
    name = input("Enter name: ")
    marks = int(input("Enter marks: "))
    students[roll] = {"name": name, "marks": marks}
above = {roll: info for roll, info in students.items() if info["marks"] > 75}
print("Students with marks above 75:")
for roll in above:
    print(roll, above[roll]["name"], above[roll]["marks"])

Sample output:

Enter number of students: 5
Enter roll number: 101
Enter name: Aman
Enter marks: 82
Enter roll number: 102
Enter name: Riya
Enter marks: 68
Enter roll number: 103
Enter name: Karan
Enter marks: 91
Enter roll number: 104
Enter name: Neha
Enter marks: 75
Enter roll number: 105
Enter name: Sita
Enter marks: 79
Students with marks above 75:
101 Aman 82
103 Karan 91
105 Sita 79

Method 5: Two simple dictionaries

Some books keep one dictionary for names and one for marks, both using the roll number as the key. It is easy to understand, but the data of a single student is split into two places.

n = int(input("Enter number of students: "))
names = {}
marks = {}
for i in range(n):
    roll = int(input("Enter roll number: "))
    names[roll] = input("Enter name: ")
    marks[roll] = int(input("Enter marks: "))
print("Students with marks above 75:")
for roll in names:
    if marks[roll] > 75:
        print(names[roll])

Sample output:

Enter number of students: 3
Enter roll number: 30
Enter name: Z
Enter marks: 80
Enter roll number: 10
Enter name: Y
Enter marks: 90
Enter roll number: 20
Enter name: X
Enter marks: 50
Students with marks above 75:
Z
Y

Method 6: Data written directly in the program

When the data is fixed, you can write the dictionary directly and skip the input loop. This is handy for testing the filter part of the program.

students = {
    101: {"name": "Aman", "marks": 82},
    102: {"name": "Riya", "marks": 68},
    103: {"name": "Karan", "marks": 91},
    104: {"name": "Neha", "marks": 75},
    105: {"name": "Sita", "marks": 79},
}
print("Students with marks above 75:")
for roll in students:
    if students[roll]["marks"] > 75:
        print(students[roll]["name"])

Output:

Students with marks above 75:
Aman
Karan
Sita

Variation: names in order of marks

The sorted() function with a key arranges the students from the highest marks to the lowest.

n = int(input("Enter number of students: "))
students = {}
for i in range(n):
    roll = int(input("Enter roll number: "))
    name = input("Enter name: ")
    marks = int(input("Enter marks: "))
    students[roll] = {"name": name, "marks": marks}
ranked = sorted(students.items(), key=lambda item: item[1]["marks"], reverse=True)
for roll, info in ranked:
    if info["marks"] > 75:
        print(info["name"], info["marks"])

Output for the five students above:

Karan 91
Aman 82
Sita 79

Variation: average and topper

The same dictionary answers other questions too. Add up all the marks for the average, and remember the roll number with the highest marks for the topper.

n = int(input("Enter number of students: "))
students = {}
for i in range(n):
    roll = int(input("Enter roll number: "))
    name = input("Enter name: ")
    marks = int(input("Enter marks: "))
    students[roll] = {"name": name, "marks": marks}
total = 0
best_roll = None
for roll in students:
    total = total + students[roll]["marks"]
    if best_roll is None or students[roll]["marks"] > students[best_roll]["marks"]:
        best_roll = roll
print("Average marks:", total / n)
print("Topper:", students[best_roll]["name"], students[best_roll]["marks"])

Output for the five students above:

Average marks: 79.0
Topper: Karan 91

Variation: a structure in C

In C a struct groups the roll number, name and marks of one student into a single unit, which is the closest thing to the inner dictionary.

C program using struct
#include <stdio.h>
struct Student {
    int roll;
    char name[20];
    int marks;
};
int main() {
    int n;
    printf("Enter number of students: ");
    scanf("%d", &n);
    struct Student s[50];
    for (int i = 0; i < n; i++) {
        printf("Enter roll number: ");
        scanf("%d", &s[i].roll);
        printf("Enter name: ");
        scanf("%s", s[i].name);
        printf("Enter marks: ");
        scanf("%d", &s[i].marks);
    }
    printf("Students with marks above 75:\n");
    int count = 0;
    for (int i = 0; i < n; i++) {
        if (s[i].marks > 75) {
            printf("%s\n", s[i].name);
            count++;
        }
    }
    if (count == 0) {
        printf("No student has marks above 75\n");
    }
    return 0;
}

Common Mistakes

  1. Reading marks as text. input() returns a string. Without int(), the comparison marks > 75 raises a TypeError in Python 3. Even if you compare two strings, the answer is wrong, because text is compared character by character: “9” > “75” is True.
  2. Using >= instead of >. “Above 75” means 76 and more. A student with exactly 75 must not be printed.
  3. Repeating a roll number. A dictionary key is unique. If you enter roll 1 twice, the second student replaces the first, and the dictionary holds fewer than n entries. The interactive tool refuses repeated roll numbers for this reason.
  4. Printing the whole dictionary. print(students) prints every student with braces and quotes. To print only the names, loop and pick ["name"].
  5. Forgetting the second key. students[roll] is the inner dictionary. You still need ["marks"] or ["name"] to reach the actual value.
  6. Asking for a missing key. students[999] raises a KeyError if 999 was never entered. Use students.get(999), which returns None instead of failing.
  7. Printing the message inside the loop. Print “no student above 75” only once, after the loop, using a counter.
  8. Using a name with a space in C or Java. scanf("%s") and sc.next() read only one word. Use a single-word name, or read the whole line in a separate step.

Practice Questions

  1. Display the names of students whose marks are between 60 and 75.
  2. Find and print the name of the student with the highest marks.
  3. Search for a roll number entered by the user and print the name and marks, or “not found”.
  4. Give 5 grace marks to every student with marks below 40, and print the updated dictionary.
  5. Count how many students scored above 75 and below 33.
  6. Print each student’s grade: A for above 90, B for 75 to 90, C for 50 to 74 and D below 50.

Frequently Asked Questions

What is a dictionary in Python?

A dictionary is a collection of key: value pairs written in curly braces. You use the key to get the value, for example students[101].

How do you create a dictionary of students in Python?

Start with students = {}. Then, for each student, add an entry with students[roll] = {"name": name, "marks": marks}.

Why is the roll number used as the key?

Every student has a different roll number, and dictionary keys must be unique. That makes the roll number the perfect key. A name is a poor key because two students can share it.

How do I display the names of students with marks above 75?

Loop over the dictionary and print the name whenever students[roll]["marks"] > 75. The programs in this post show the loop in Python, C and Java.

Does the order of the dictionary stay the same?

Yes. From Python 3.7 a dictionary remembers the order in which keys were added, so students are printed in the order you entered them. In Java the program uses LinkedHashMap for the same reason, because a plain HashMap does not promise an order.

What happens if I enter the same roll number twice?

The new entry replaces the old one. The dictionary then holds fewer than n students, and the first student is lost.

Why do I get a TypeError when comparing marks?

The marks were read with input(), so they are text. Convert them with int(input(...)) before comparing them with 75.

What is the difference between a list of lists and a dictionary of dictionaries here?

In a list of lists you find a student by position, for example students[2]. In a dictionary you find a student by roll number, for example students[103], which is closer to how real records are looked up.

How do I write this program in C if there is no dictionary?

Use three arrays that share an index, or an array of structures. Index i then holds the roll number, name and marks of one student.

What is the time complexity of this program?

Both loops visit each student once, so the program takes O(n) time. Adding an entry to a dictionary takes constant time on average.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top