+ 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)
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.
+ 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...
+ 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...