Code Explanation:
1) import asyncio
Imports Python’s asyncio library.
Provides an event loop and tools to run asynchronous coroutines.
2) async def f():
Defines a coroutine function f.
Inside f:
await asyncio.sleep(0.1)
return 7
await asyncio.sleep(0.1) suspends the coroutine for 0.1 seconds without blocking the whole program.
After the wait, it returns 7.
3) async def g():
Defines another coroutine function g.
Inside g:
return await f() + 3
Calls f() (returns a coroutine object).
await f() suspends until f completes and gives result 7.
Adds 3 to it → result = 10.
4) print(asyncio.run(g()))
asyncio.run(g()):
Creates a new event loop.
Runs coroutine g() until it finishes.
Returns the result (10).
print(...) prints the result.
Final Output
10
Download Book - 500 Days Python Coding Challenges with Explanation
.png)

0 Comments:
Post a Comment