Bird
0
0

Why does this code print only the last value of the list?

hard📝 Conceptual Q10 of 15
Python - For Loop
Why does this code print only the last value of the list?
values = [1, 2, 3]
for v in values:
    pass
print(v)
ABecause pass skips all iterations
BBecause the loop variable retains the last item after loop ends
CBecause print is outside the loop and cannot access v
DBecause the list is empty
Step-by-Step Solution
Solution:
  1. Step 1: Understand loop variable scope

    In Python, the loop variable keeps the last value after the loop finishes.
  2. Step 2: Analyze the code behavior

    Since 'pass' does nothing, the loop runs fully, and print(v) prints the last item.
  3. Final Answer:

    Because the loop variable retains the last item after loop ends -> Option B
  4. Quick Check:

    Loop variable keeps last value after loop [OK]
Quick Trick: Loop variable keeps last value after loop ends [OK]
Common Mistakes:
MISTAKES
  • Thinking pass skips loop
  • Assuming print can't access v
  • Believing list is empty

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes