Code Explanation:
1️⃣ Importing the Module
import threading
Explanation
Imports Python’s threading module.
This module allows execution of multiple threads simultaneously.
2️⃣ Defining the Function
def task(n):
Explanation
A function named task is defined.
It takes one argument n.
Each thread will call this function with a different value.
3️⃣ Printing the Value
print(n)
Explanation
This prints the value passed to the function.
Each thread will print its own number.
4️⃣ Loop Creation
for i in range(3):
Explanation
Loop runs 3 times.
Values of i will be:
0, 1, 2
5️⃣ Creating and Starting Threads
threading.Thread(target=task, args=(i)).start()
Explanation (VERY IMPORTANT ⚠️)
✔ Thread Creation
threading.Thread(...) creates a new thread.
target=task → thread will run task() function.
❌ Mistake in Arguments
args=(i) is NOT a tuple.
Python treats (i) as just an integer, not a tuple.
๐ Correct tuple should be:
args=(i,)
⚠️ What Happens Due to Mistake?
Thread expects arguments as an iterable (tuple/list).
But (i) is an int, not iterable.
So Python raises an error.
❌ Runtime Error
TypeError: 'int' object is not iterable
Final Output:
Error

0 Comments:
Post a Comment