๐น Step 1: Tuple creation
t = (1000, 2000, 3000)A tuple with three integer objects is created.
๐น Step 2: Loop starts
for i in t:The loop assigns each element of the tuple to i, one by one:
-
1st iteration → i = 1000
-
2nd iteration → i = 2000
-
3rd iteration → i = 3000
๐น Step 3: is operator check
if i is 2000:⚠️ This is the tricky part.
is checks identity → Are both variables pointing to the same object in memory?
-
It does NOT check value equality.
Even though i looks like 2000, it may not be the same object as the literal 2000.
๐ Large integers (like 2000) are not reliably cached in Python.
๐น Step 4: Condition result
i is 2000 → False
-
So print("Found") is never executed
๐น Final Output
(no output)Key Takeaway
| Operator | Meaning |
|---|---|
| == | Compare values |
| is | Compare memory location |
✔ Correct way to check value:


0 Comments:
Post a Comment