Python · Chapter 2.11 · Pro level

Capstone: Report Card Manager (lists of dicts, lambda, comprehensions, modules)

कैपस्टोन: रिपोर्ट कार्ड मैनेजर (dicts की list, lambda, comprehension, modules)

Real software = small tools working together. We store each student as a dictionary inside a list, write functions to calculate, use a list comprehension to build a new list in one line ([s["name"] for s in students if s["marks"] >= 60]), sort with a lambda (sorted(students, key=lambda s: s["marks"], reverse=True)) and use the math module to round up. This is exactly how school ERP and results websites work!

असली सॉफ़्टवेयर = छोटे-छोटे टूल्स का मिलकर काम करना। हर छात्र को list के अंदर एक dictionary में रखते हैं, गणना के लिए functions लिखते हैं, list comprehension से एक लाइन में नई list बनाते हैं ([s["name"] for s in students if s["marks"] >= 60]), lambda से sort करते हैं (sorted(students, key=lambda s: s["marks"], reverse=True)) और ऊपर गोल करने के लिए math module लेते हैं। स्कूल ERP और रिज़ल्ट वेबसाइट्स ठीक ऐसे ही काम करती हैं!

In the classroom you can run and edit this code in your browser, get hints and mark the chapter as done. क्लास में यह code ब्राउज़र में चलाकर बदल सकते हो।

Words and symbols in this chapter

[{"name": "Riya", "marks": 91}, ...]
A list where every item is a dictionary — like rows of a table.
ऐसी list जिसका हर item एक dictionary है — टेबल की rows जैसा।
[x for x in items if ...]
List comprehension: build a filtered list in one line.
List comprehension: एक लाइन में छँटी हुई list बनाना।
lambda s: s["marks"]
A tiny one-line function, here telling sorted what to sort by.
एक लाइन का छोटा function, यहाँ sorted को बताता है कि किस आधार पर sort करना है।
import math / math.ceil(x)
Brings extra tools; ceil rounds UP (72.3 → 73).
अतिरिक्त टूल लाता है; ceil ऊपर की ओर गोल करता है (72.3 → 73)।

Ready example

import math

students = [
    {"name": "Riya", "marks": 91},
    {"name": "Aman", "marks": 58},
    {"name": "Kabir", "marks": 76},
]

def grade(m):
    if m >= 90:
        return "A+"
    elif m >= 60:
        return "B"
    return "Needs practice"

for s in sorted(students, key=lambda s: s["marks"], reverse=True):
    print(f"{s['name']:<6} {s['marks']:>3}  {grade(s['marks'])}")

passed = [s["name"] for s in students if s["marks"] >= 60]
average = sum(s["marks"] for s in students) / len(students)
print("Passed:", passed)
print("Class average (rounded up):", math.ceil(average))

What you will see: A neat ranked report card, the pass list and the class average appear in the terminal.

Terminal में रैंक वाला रिपोर्ट कार्ड, पास वालों की list और क्लास का औसत दिखेगा।

Line by line

students = [ {...}, {...}, {...} ]
Three student records.
तीन छात्रों के रिकॉर्ड।
def grade(m): ...
A reusable function that turns marks into a grade.
दोबारा इस्तेमाल होने वाला function जो marks को grade में बदलता है।
sorted(students, key=lambda s: s["marks"], reverse=True)
Topper first: sort by marks, highest to lowest.
टॉपर सबसे ऊपर: marks के हिसाब से, बड़े से छोटे।
passed = [s["name"] for s in students if s["marks"] >= 60]
Names of everyone with 60+ in one line.
60+ वाले सबके नाम एक लाइन में।

Common mistake

✗ Wrong

sorted(students, key=students["marks"])

✓ Right

sorted(students, key=lambda s: s["marks"])

key needs a function — use a lambda that picks the field from each item.

key को एक function चाहिए — lambda लिखो जो हर item से field निकाले।

Your task

Find the topper with max(..., key=lambda ...), build a list of passed names with a comprehension, and print the class average.

max(..., key=lambda ...) से टॉपर निकालो, comprehension से पास हुए नामों की list बनाओ और क्लास का औसत print करो।

  1. topper = max(students, key=lambda s: s["marks"]) then print("Topper:", topper["name"]).
    topper = max(students, key=lambda s: s["marks"]) फिर print("Topper:", topper["name"])।
  2. passed = [s["name"] for s in students if s["marks"] >= 60] then print(passed).
    passed = [s["name"] for s in students if s["marks"] >= 60] फिर print(passed)।
  3. average = sum(s["marks"] for s in students) / len(students) and print it.
    average = sum(s["marks"] for s in students) / len(students) और print करो।
Starter code for the task
students = [
    {"name": "Riya", "marks": 91},
    {"name": "Aman", "marks": 58},
    {"name": "Kabir", "marks": 76},
    {"name": "Sara", "marks": 64},
]

# TODO 1: topper = max(students, key=lambda s: s["marks"])  aur topper ka naam print karo
# TODO 2: passed = [ ... for s in students if s["marks"] >= 60 ]  (sirf naam) aur print karo
# TODO 3: average nikalo (sum / len) aur print karo

Do this task in the classroom: your code is checked automatically and you get hints if something is missing.

Recap

Quick quiz

What does [n * 2 for n in [1, 2, 3]] give?

[n * 2 for n in [1, 2, 3]] क्या देगा?

  1. [1, 2, 3]
  2. [2, 4, 6]
  3. 6
  4. [1, 2, 3, 1, 2, 3]
Show the answer

Answer: B ([2, 4, 6])

The comprehension doubles every item.

Comprehension हर item को दोगुना करती है।

Practice problems for this chapter

Solve them in the Practice Zone, where each answer is checked automatically and two hints are available.