Complete Python Series · Part 27 of 29
Assignment 14-revenue-analyzer
NextGen CTO — Python Assignments
A collection of practical Python exercises based on real-world scenarios from the NextGen CTO learning platform. Each assignment includes starter code, hints, a complete solution, and an explanation.
Exercise: NextGen CTO Course Revenue Analyzer
Build a course revenue reporting system using dictionary comprehensions. Learn to unpack tuples, filter records, calculate revenue, and construct key-value pairs.
Problem statement
NextGen CTO wants to generate a revenue report for its courses. Each record contains a course name, price per enrollment in INR, and number of paid enrollments. Calculate the revenue generated by each course, excluding courses with zero paid enrollments.
Task requirements
- Use the provided list of course tuples.
- Unpack each tuple into name, price, and enrollments.
- Create a dictionary named
course_revenueusing a dictionary comprehension. - Use the course name as the key and price multiplied by enrollments as the value.
- Include only courses with positive paid enrollment counts.
- Print the resulting dictionary.
Constraints
- Use a dictionary comprehension instead of an explicit
forloop. - Do not modify the original list.
- Assume unique course names, non-negative enrollment counts, and a fixed price per paid enrollment.
- Do not use external libraries.
Good luck trying the exercise!
Starter code
# Format: (Course Name, Price, Paid Enrollments)
courses = [
("Python", 999, 100),
("DSA", 1499, 0),
("System Design", 1999, 50),
("DBMS", 799, 0),
("AI Engineering", 2499, 20)
]
# TODO: Create course_revenue using a dictionary comprehension
course_revenue = {}
# Print the result
print(course_revenue)
Hints
- A dictionary comprehension follows the pattern
{key: value for item in iterable if condition}. - Unpack each tuple using
for name, price, enrollments in courses. - Multiply price by enrollments to calculate revenue.
- Filter records using
if enrollments > 0.
Solution: NextGen CTO Course Revenue Analyzer
Use a dictionary comprehension to combine filtering and revenue calculation into one expression.
# Format: (Course Name, Price, Paid Enrollments)
courses = [
("Python", 999, 100),
("DSA", 1499, 0),
("System Design", 1999, 50),
("DBMS", 799, 0),
("AI Engineering", 2499, 20)
]
# Calculate revenue using dictionary comprehension
course_revenue = {
name: price * enrollments
for name, price, enrollments in courses
if enrollments > 0
}
# Print the result
print(course_revenue)
Expected output
{'Python': 99900, 'System Design': 99950, 'AI Engineering': 49980}
Solution explanation
- Lines 1–8: Define the list of tuples containing course names, prices and enrollment counts.
- Lines 11–15: Build the dictionary using a comprehension. Line 12 uses the course name as the key and multiplies price by enrollments for the value. Line 13 unpacks each tuple. Line 14 excludes courses with zero enrollments.
- Line 18: Print the resulting dictionary.
Test cases
Prices and enrollment counts are illustrative. Revenue here means gross course sales before discounts, refunds, or taxes.