Student Marks Analysis

AIM

To write a Python program to display the names and marks of students who scored more than 75 from a given dictionary.

Key Concepts:

  • Dictionary: Key-value pairs for storing student data
  • Iteration: Looping through dictionary items
  • Conditional Logic: Filtering based on marks criteria

ALGORITHM

  1. Start
  2. Create a dictionary with student names as keys and marks as values
  3. Loop through each student in the dictionary
  4. Check if the student's marks are greater than 75
  5. If yes, print the student name and marks
  6. Repeat for all students
  7. Stop

PROGRAM

# Dictionary of students and marks
students = {
    "Alice": 82,
    "Bob": 70,
    "Charlie": 90,
    "David": 65,
    "Eva": 78,
    "Frank": 95,
    "Grace": 73,
    "Henry": 88
}

print("Students with marks above 75:")
print("-" * 30)

# Loop through dictionary
for name, marks in students.items():
    if marks > 75:
        print(f"{name}: {marks}")

# Additional analysis
print("\n" + "=" * 30)
print("STATISTICAL ANALYSIS")
print("=" * 30)

# Count students above 75
count_above_75 = sum(1 for marks in students.values() if marks > 75)
total_students = len(students)

print(f"Total students: {total_students}")
print(f"Students above 75: {count_above_75}")
print(f"Percentage: {(count_above_75/total_students)*100:.1f}%")

# Find highest and lowest marks
highest_marks = max(students.values())
lowest_marks = min(students.values())

print(f"Highest marks: {highest_marks}")
print(f"Lowest marks: {lowest_marks}")

# Calculate average
average_marks = sum(students.values()) / len(students)
print(f"Average marks: {average_marks:.2f}")

OUTPUT

Students with marks above 75:

------------------------------

Alice: 82

Charlie: 90

Eva: 78

Frank: 95

Henry: 88

==============================

STATISTICAL ANALYSIS

==============================

Total students: 8

Students above 75: 5

Percentage: 62.5%

Highest marks: 95

Lowest marks: 65

Average marks: 78.88

CONCLUSION

The program successfully demonstrates dictionary manipulation and conditional filtering. It efficiently identifies students with marks above 75 and provides additional statistical analysis including percentage calculations, highest/lowest marks, and average computation.

VIVA QUESTIONS

  1. What is a dictionary in Python?

    A dictionary is a collection of key-value pairs that is unordered, changeable, and indexed. Keys must be unique and immutable.

  2. How do you iterate through a dictionary?

    Use dict.items() to get both key and value, dict.keys() for keys only, or dict.values() for values only.

  3. What is the difference between dict.items() and dict.values()?

    dict.items() returns key-value pairs as tuples, while dict.values() returns only the values.

  4. How would you find the student with highest marks?

    Use max(students, key=students.get) to get the key with maximum value, or max(students.values()) for just the value.

  5. Can dictionary keys be modified?

    No, dictionary keys are immutable. You cannot change a key directly; you must delete the old key-value pair and add a new one.

  6. How do you add a new student to the dictionary?

    Use students["NewStudent"] = marks or students.update({"NewStudent": marks}).

  7. What happens if you try to access a non-existent key?

    Python raises a KeyError. Use dict.get(key, default) to avoid this error.

  8. How would you sort students by marks?

    Use sorted(students.items(), key=lambda x: x[1], reverse=True) to sort by marks in descending order.

Related Resources

💡 Pro Tip

Use dict.get(key, default) to safely access dictionary values without KeyError.

🎯 Key Learning

Dictionary comprehension: {k: v for k, v in dict.items() if condition}