Skip to main content

Practical Exercises

Turning Data into Systems

Up until now, you’ve learned about lists, tuples, sets, and dictionaries. But data structures are only powerful when they’re applied to solve real problems. Imagine being a hacker who needs to manage contacts or track student grades. You don’t want scattered variables but you want a system.

This chapter introduces two practical projects: a Contact Book and a Student Grades Manager. Both exercises show how Python’s data structures can be combined into small but functional applications. They’re not just practice but they’re blueprints for real‑world utilities.


Why These Projects Matter

  • Integration of Data Structures: Lists, dictionaries, and sets work together to organize complex information.
  • Real‑World Mapping: Contacts and grades are everyday problems solved by mapping identifiers (names, IDs) to values (phone numbers, marks).
  • Scalability: These small projects mirror larger systems like CRMs or school databases.
  • Problem‑Solving: You learn to break down requirements into logical steps and implement them with Python’s building blocks.

Exercise 1: Contact Book

A contact book stores names and phone numbers. Dictionaries are perfect for this because they map names (keys) to numbers (values).

print("Contact Book")
contacts = {}

while True:
    action = input("Choose action: add, search, delete, exit: ")

    if action == "add":
        name = input("Enter name: ")
        number = input("Enter phone number: ")
        contacts[name] = number
        print("Contact added.")

    elif action == "search":
        name = input("Enter name to search: ")
        if name in contacts:
            print(f"{name}: {contacts[name]}")
        else:
            print("Contact not found.")

    elif action == "delete":
        name = input("Enter name to delete: ")
        if name in contacts:
            del contacts[name]
            print("Contact deleted.")
        else:
            print("Contact not found.")

    elif action == "exit":
        break
    else:
        print("Invalid action.")
  • Why? This exercise teaches dictionary operations (add, search, delete) and user interaction.
  • Real‑World Analogy: Like a phonebook app, but built from scratch.

Exercise 2: Student Grades Manager

A grades manager stores student names and their marks. Dictionaries again provide the mapping, while loops and conditionals handle logic.

print("Student Grades Manager")
grades = {}

while True:
    action = input("Choose action: add, view, average, exit: ")

    if action == "add":
        name = input("Enter student name: ")
        marks = int(input("Enter marks: "))
        grades[name] = marks
        print("Grade added.")

    elif action == "view":
        for student, marks in grades.items():
            print(f"{student}: {marks}")

    elif action == "average":
        if grades:
            avg = sum(grades.values()) / len(grades)
            print(f"Class Average: {avg}")
        else:
            print("No grades available.")

    elif action == "exit":
        break
    else:
        print("Invalid action.")
  • Why? This exercise integrates input/output, dictionary iteration, and arithmetic operations.
  • Real‑World Analogy: Like a simplified school grading system.

The Hacker’s Notebook

  • Dictionaries are ideal for mapping identifiers to values, names to numbers, students to grades. Loops and conditionals turn static data into interactive systems.
  • Projects reveal how small building blocks combine into functional applications. Error handling (like missing contacts or empty grade lists) is critical for reliability.

Hacker’s Mindset: treat these exercises as prototypes. Today it’s a contact book; tomorrow it’s a CRM. Today it’s a grades manager; tomorrow it’s an analytics dashboard.


Tips, Tricks, Roadmaps, Resources, Networking, Motivation, Guidance, and Cool Stuff ♥

Updated on Jan 2, 2026