Python Practice / Exercise

Explore Python’s standard library

Choose counting, parsing or statistics; run the example, change the data and inspect boundary cases.

You will learn to

  • Choose a library by its data contract
  • Validate parsed data before using it
  • Distinguish mean and median

Before you start

Basic programming and the introductory activities

Choose the smallest useful tool

A library should solve a specific problem. Counter handles counting, json handles serialization, and statistics handles common summaries. They ship with Python, so these examples do not need pip, external credentials or access to your files. Selecting a library changes the runnable example below.

Parsing is only the first boundary

json.loads checks JSON syntax. It does not promise that the result is an object or that name is a string. Change the fixture to an array and observe the explicit rejection. Then try malformed JSON and read the exception. Do not use eval to parse a data format.

Summaries depend on the question

The sensor readings [10, 11, 12, 13, 1000] have mean 209.2 and median 12. Both calculations are correct. Whether the last reading is a real event or a sensor fault requires evidence outside this list. Deleting the outlier because it is inconvenient changes the claim you can make.

Your challenge

Add a punctuation policy to Counter, a maximum name length to the JSON example, or a range field to the summary. Preserve the empty-input behavior. A working imported module is not evidence that every Python package works in a browser; these examples use the standard library only.

Try it yourself

Enable JavaScript for this interactive activity. You can read all lesson explanations above without it.

Continue exploring