Code Explanation:
1️⃣ Importing the Module
import threading
Explanation
Imports Python’s threading module.
Used to create and manage threads.
2️⃣ Defining the Function
def task(i):
Explanation
A function named task is defined.
It takes one argument i.
Each thread will print a value.
3️⃣ Printing the Value
print(i)
Explanation
Prints the value passed to the function.
Output depends on the value of i.
4️⃣ Loop Execution
for i in range(3):
Explanation
Loop runs 3 times.
Values of i:
0, 1, 2
5️⃣ Creating a Thread
t = threading.Thread(target=task, args=(i,))
Explanation
A new thread is created in each iteration.
target=task → thread will execute task(i).
args=(i,) → passes current value of i as argument.
⚠️ (i,) is a tuple (correct syntax).
6️⃣ Starting the Thread
t.start()
Explanation
Starts the thread.
The thread executes task(i) and prints the value.
7️⃣ Joining the Thread (⚠️ Important)
t.join()
Explanation
Main thread waits for the current thread to finish before continuing.
This is inside the loop → so threads run one by one (not parallel).
๐ Execution Flow
Iteration 1:
Thread runs → prints 0
Main thread waits
Iteration 2:
Thread runs → prints 1
Main thread waits
Iteration 3:
Thread runs → prints 2
Main thread waits
๐ค Final Output
0
1
2
