Lesson 18 of 19 · 3 min · practice
Practice Questions
Q1. Write a function calculate_grade(score) that returns "A" (≥90), "B" (≥75), "C" (≥60), "D" (≥45), or "F". Include a complete docstring with parameters, return value, and an example. Then write class_report(scores_dict) that takes {"Priya": 85, "Ahmed": 92, "Ravi": 45} and prints each student's name and grade.
Q2. Write stats_summary(*numbers) using *args that accepts any number of values and returns a dictionary with keys: "count", "total", "mean", "minimum", "maximum". Test with both 3 numbers and 7 numbers in separate calls.
Q3. You have student data as tuples: [("Priya", 85), ("Ahmed", 92), ("Ravi", 78), ("Sara", 45), ("Meera", 60)]. Write a function that converts this list into a list of dictionaries [{"name": "Priya", "score": 85, "grade": "B"}, ...] adding the letter grade for each student.
Q4. Write word_frequency(sentence) that takes a string, converts it to lowercase, splits by spaces, and returns a dictionary of word counts. Test with: "the cat sat on the mat the cat". Expected: {"the": 3, "cat": 2, "sat": 1, "on": 1, "mat": 1}.
Q5. Using set operations on two club membership sets: Coding Club {"Priya","Ahmed","Ravi","Sara"} and Maths Club {"Ahmed","Ravi","Arjun","Riya"}, find: (a) members in BOTH clubs, (b) members in Coding Club only, (c) all unique members, (d) members in exactly ONE club. Print each result with a label.
Q6. Write build_profile(**kwargs) that accepts any keyword arguments and returns a formatted string like "Name: Priya | Age: 20 | Branch: CSE | CGPA: 3.75". Test with three different combinations of keyword arguments.
Q7. Using a list comprehension, create a list of squares of numbers 1–20. Then write a second comprehension to filter and keep only the squares that are greater than 100. Print both lists.
Q8. Write a lambda function and use it with sorted() to sort this list of dictionaries by score in descending order: [{"name": "Ravi", "score": 78}, {"name": "Priya", "score": 85}, {"name": "Ahmed", "score": 92}].
Q9. Write group_by_grade(scores_dict) that takes {"Priya": 85, "Ahmed": 92, "Ravi": 78, "Sara": 45} and returns a nested dictionary grouping student names by grade: {"A": ["Ahmed"], "B": ["Priya", "Ravi"], "F": ["Sara"]}. Use setdefault to build the grouped dictionary.
Q10. You have a list of product dictionaries:
products = [
{"name": "Laptop", "price": 55000, "category": "Electronics"},
{"name": "T-Shirt", "price": 499, "category": "Clothing"},
{"name": "Phone", "price": 25000, "category": "Electronics"},
{"name": "Jeans", "price": 1299, "category": "Clothing"},
]
Write code using list comprehension and dictionary comprehension to: (a) get names of all products under ₹10000, (b) build {"Electronics": [...], "Clothing": [...]} grouping product names by category, (c) find the most expensive product in each category.
