0

Question with Python for loop, I'm a beginner

I don't understand why if my list name is words I can set a for loop with the counter variable called words just like the list. How does Python chooses which variable is the list or the counter if they have the same name?? I know it's better practice if I name them different, I just wanna know why Python allows me to call them the same?? https://code.sololearn.com/cb9n0ZWX9MeT/?ref=app

28th Jun 2017, 3:35 AM
José Enrique Lugo Piñón
José Enrique Lugo Piñón - avatar
1 Odpowiedź
+ 2
words = ["hello", "world", "spam", "eggs"] for e in words: print(e + "!") yes i agree this is more readable. its because the variable words used in the for loop has a smaller scope so it doesnt affect the variable words outside the for loop
28th Jun 2017, 3:48 AM
David Sebastian Keshvi Illiakis
David Sebastian Keshvi Illiakis - avatar