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 और रिज़ल्ट वेबसाइट्स ठीक ऐसे ही काम करती हैं!
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
sortedwhat to sort by. - एक लाइन का छोटा function, यहाँ
sortedको बताता है कि किस आधार पर sort करना है। import math / math.ceil(x)- Brings extra tools;
ceilrounds 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 करो।
topper = max(students, key=lambda s: s["marks"])thenprint("Topper:", topper["name"]).topper = max(students, key=lambda s: s["marks"])फिरprint("Topper:", topper["name"])।passed = [s["name"] for s in students if s["marks"] >= 60]thenprint(passed).passed = [s["name"] for s in students if s["marks"] >= 60]फिरprint(passed)।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 karoDo this task in the classroom: your code is checked automatically and you get hints if something is missing.
Recap
- Store records as a list of dictionaries.
sorted/max/min(..., key=lambda x: ...)sort or pick by any field.- List comprehensions build new lists in one readable line.
- रिकॉर्ड्स को dictionaries की list में रखो।
sorted/max/min(..., key=lambda x: ...)किसी भी field से sort या चुनते हैं।- List comprehension एक साफ़ लाइन में नई list बनाती है।
Quick quiz
What does [n * 2 for n in [1, 2, 3]] give?
[n * 2 for n in [1, 2, 3]] क्या देगा?
Show the answer
Answer: B ([2, 4, 6])
The comprehension doubles every item.
Comprehension हर item को दोगुना करती है।
Practice problems for this chapter
- Mini Project: Class Result Manager
मिनी प्रोजेक्ट: क्लास रिज़ल्ट मैनेजर
Solve them in the Practice Zone, where each answer is checked automatically and two hints are available.