0

When I executed below code in Codecademy Labs :-

 names = [Adam,"Alex","Mariah","Martine","Columbus"]

 for Adam in names:

   print Adam

It prints:-

Columbus

Alex

Mariah

Martine

Columbus

Now, I want to ask why "Columbus" is at the top position. I know I used an empty variable "Adam" in the list and in "for loop" as same. It should be a "syntax error" due to same variable usage in list as well as in "for loop" then why the last element of that list prints out at first as well at last.

falsetru
  • 357,413
  • 63
  • 732
  • 636
Devesh Saini
  • 557
  • 5
  • 16

2 Answers2

5

Maybe you're running the same code multiple times.

>>> Adam = '' # <--- except this line.
>>> names = [Adam,"Alex","Mariah","Martine","Columbus"]
>>> for Adam in names:
...     print Adam
...

Alex
Mariah
Martine
Columbus

After the for loop Adam reference "Columbus" (the last element of the list), because target variable (Adam) is not delete after the loop:

>>> Adam
'Columbus'

Running same code produce what you see:

>>> names = [Adam,"Alex","Mariah","Martine","Columbus"]
>>> for Adam in names:
...     print Adam
...
Columbus
Alex
Mariah
Martine
Columbus

Using same variable for for target variable does not cause SyntaxError. The variable is just overwritten.

enter image description here

falsetru
  • 357,413
  • 63
  • 732
  • 636
  • seems like the python interpreter in codeacademy lab didn't have the same effect. – alvas Jan 26 '14 at 08:10
  • http://labs.codecademy.com/BvO1#:workspace, I think the only that `Adam =Colubus` is that the list is reinitialized again. – alvas Jan 26 '14 at 08:12
  • @alvas, I could reproduce. See the screenshot I just added to the answer. – falsetru Jan 26 '14 at 08:14
  • @falsetru, yeah, the `names` list got to be reinitialized for the "bug" to appear =) – alvas Jan 26 '14 at 08:16
  • @alvas, I say so from the beginning. It's not a bug. See [`for` statement](http://docs.python.org/2/reference/compound_stmts.html#the-for-statement). QUOTE: The target list is not deleted when the loop is finished, ... – falsetru Jan 26 '14 at 08:18
  • pardon the terms, it's a "gotcha"! – alvas Jan 26 '14 at 08:25
0

To answer the problem 1 of why "Columbus" prints first?

I really don't know how you did it, my attempt shows that it prints in the order that the list is initialized, see printing list in python properly for how you can manipulate prints in python:

enter image description here

The answer to the second question of empty variable, I got a normal syntax error, see how variables work in python here: Python variable declaration:

enter image description here

To replicate the OP's error, Adam = "Columbus" will only be True if the OP reinitialized thenames` list after the for loop: see http://labs.codecademy.com/BvOl/1#:workspace

enter image description here

Surely using a local Adam in looping a for-loop is not unlike using i. After the for-loop, the i will take the value of the last item in the list.

>>> alist = [1,2,3,4]
>>> for i in alist:
...     print i
... 
1
2
3
4
>>> i
4

But when the next for-loop, the i will automatically be set to the first item in the list when the loop starts. But if you reinitialize the list after the first time you ran the for-loop, now the first item in the list would take the value of the last item:

>>> i = None
>>> alist = [i,2,3,4]
>>> for i in alist:
...     print i
... 
None
2
3
4
>>> i
4
>>> alist = [i,2,3,4]
>>> alist
[4, 2, 3, 4]
Community
  • 1
  • 1
alvas
  • 115,346
  • 109
  • 446
  • 738