- 1
logic behind this:
letters = ["a","b","c"] for a in letters: print(a) Please explain "for a in letters" this line of code, what is a here? and why?
4 odpowiedzi
+ 5
Hello Anil Gurjar
https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2435/?ref=app
a is a variable which represents every item:
a gets "a"
a gets "b"
a gets "c"
You can also run your code in the code playground.
+ 9
Here a is a placeholder variable. These are not used except to designate members of the list. Using a here is poor practice because it is confusing since "a" is in the list. Placeholder variables should be obvious, e.g. for i in letters or even for _ in letters. Best is using a descriptive variable name like
for letter in letters:
print(letter)
+ 4
a is each item in the list.
+ 2
You can consider 'a' as an iterator or a loop variable which iterates over an iterable. Now the iterable could be for example lists and dictionaries.