+ 1

The variable for the Loop lesson

Did my teacher want to make this intentionally confusing? This code bugged me a lot: letters = [ 'a', 'b', 'c' ] for l in letters: print(l) He (or she) could have written letters = [ 'a', 'b', 'c' ] for MyVar in letters: print(MyVar)

2nd Mar 2019, 11:35 AM
Nick Maneck
3 Answers
+ 2
"MyVar" (capitalized) looks like a constant and doesn't give any information about what the variable contains, so I don't see why that would be a better name than "l". "letter" seems to be an adequate name.
2nd Mar 2019, 12:13 PM
Anna
Anna - avatar
+ 1
consider it as: for element in element: where element is assumption variable, so you are free to give it any name you want, like; for x in letters, or y...
2nd Mar 2019, 11:59 AM
Ahmad Ali
Ahmad Ali - avatar
+ 1
In general, to loop through all elements, we need a indexing variable. I feel 'I' stand for indexing. U can find 'I' as example in many 'for' loop examples...
2nd Mar 2019, 12:52 PM
Kuri
Kuri - avatar