Guide Python Intermediate

5.18 Hands-on Exercises

Practice programs and mini projects for reinforcing Python data type concepts — type counting, deduplication, the mutable default bug, JSON parsing, and a small type profiler.

2 min read

Practice Programs

  • Write a function that takes a list of mixed types and returns counts of how many are int, str, and float.
  • Given a list with duplicate IP addresses, return only the unique ones using a set.
  • Write a function demonstrating the mutable-default-argument bug, then fix it.
  • Parse a small JSON config string and print the type of each top-level value.
  • Write a script that shows id() staying the same after list.append() but changing after tuple concatenation.

Mini Projects

  • Config loader — read a dict of settings, validate each value’s type with isinstance(), and raise a clear error for any mismatch.
  • IP-address deduplicator — read a list of IPs from a (simulated) log file and report unique vs total counts using a set.
  • Type profiler — given any object, print its type, id, and whether it’s mutable or immutable (based on a lookup table of known types).

Quick Interview Answer

“These exercises reinforce the chapter’s core ideas hands-on: counting types in a mixed list exercises type()/isinstance(), deduplicating IPs exercises set, the mutable-default-argument fix exercises function default semantics, and the type profiler exercises identity (id()) plus the mutable/immutable distinction from 5.9 Mutable vs Immutable Types together.”

Common Mistakes

  • Skipping the mutable-default exercise as “just theory” — it’s one of the most common real bugs in production Python code.
  • Building the type profiler with a hardcoded if/elif chain per type instead of a lookup table, making it harder to extend.

Add More Questions to This Guide

Know a question that should be here? Share it and help the community!

Open Google Form