Code Explanation:
1) import asyncio
Brings in Python’s asynchronous I/O library.
We’ll use it to run coroutines with an event loop.
2) async def f():
Defines coroutine function f.
Calling f() returns a coroutine object (doesn’t run yet).
Body
return 5
When awaited, f immediately finishes and yields 5.
3) async def g():
Defines another coroutine g.
Body
return await f() + 10
f() creates a coroutine; await f() runs it and gets its result.
Operator precedence makes this equivalent to:
return (await f()) + 10
So: await f() → 5, then add 10 → return 15.
4) print(asyncio.run(g()))
asyncio.run(g()):
Creates an event loop,
Runs coroutine g() to completion,
Returns its result.
print(...) prints that result.
Output
15
Download Book - 500 Days Python Coding Challenges with Explanation
.png)

0 Comments:
Post a Comment