Skip to content

Beyond the Basics Exercises (Modules 10-12)

Module 10: Collections and Itertools

Exercise 1: Count words

Count how many times each word appears in a sentence.

Hint

Split the sentence and feed the list to collections.Counter.

One possible solution
from collections import Counter

sentence = "python book python docs"
counts = Counter(sentence.split())
print(counts)

Module 11: Datetime and JSON

Exercise 2: JSON round trip

Create a dictionary, serialize it to JSON text, and parse it back.

Hint

Use json.dumps and json.loads.

One possible solution
import json

data = {"chapter": "datetime", "level": "beginner"}
raw = json.dumps(data)
parsed = json.loads(raw)
print(parsed["chapter"])

Module 12: Async and Typing

Exercise 3: Async await

Create an async function that sleeps for half a second and returns "ok".

Hint

Use asyncio.sleep(0.5).

One possible solution
import asyncio


async def ping() -> str:
    await asyncio.sleep(0.5)
    return "ok"


print(asyncio.run(ping()))