logic behind this: | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 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?

6th Jun 2020, 3:38 PM
Anil Gurjar
Anil Gurjar - avatar
4 Answers
+ 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.
6th Jun 2020, 3:46 PM
Denise Roßberg
Denise Roßberg - avatar
+ 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)
6th Jun 2020, 3:51 PM
David Ashton
David Ashton - avatar
+ 4
a is each item in the list.
6th Jun 2020, 3:42 PM
Seb TheS
Seb TheS - avatar
+ 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.
6th Jun 2020, 3:54 PM
Avinesh
Avinesh - avatar