Chapter Practice Arena
All ChaptersData, APIs, and Concurrency Problems
Targeted practice challenges specifically curated for this chapter. Inspect hints, verify outputs, and check Pythonic solutions.
Difficulty:
Chapter:
Showing 6 challenges · 20 total · Page 1 of 4
Create an async function double(value) that awaits asyncio.sleep(0) and returns value * 2. Use asyncio.gather() to call it with 3 and 5, then print the results.
Starter Code
import asyncio
async def double(value):
# Yield asynchronously and return value * 2.
pass
async def main():
# Gather both results and print the returned list.
pass
asyncio.run(main())Expected Output:[6, 10]
Write a coroutine that raises ValueError('Bad data'). In main(), await it inside a try/except block and print the exception message prefixed with Error:.
Starter Code
import asyncio
async def read_data():
# Raise ValueError('Bad data').
pass
async def main():
# Await read_data() and catch ValueError.
pass
asyncio.run(main())Expected Output:Error: Bad data
Use urlencode() to encode the dictionary {'page': 2, 'sort': 'name'} and print the result.
Starter Code
from urllib.parse import urlencode # Build and print the query string.
Expected Output:page=2&sort=name