[Tutor] Confusion about name in the target list of for loop

Manprit Singh manpritsinghece at gmail.com
Fri Aug 28 07:18:10 EDT 2020


Dear sir ,
Consider a problem of finding the average of numbers(1,2, 3, 4, 5). if this
is solved using the for loop in this way,  is it correct ?

s = 0
for i in range(1, 6):
    s = s + i
print(s / i)
3.0

As we know, the for loop in the above example will produce the sum of
numbers,
1, 2, 3, 4, 5. Now as the last value of variable i (which is a variable in
the target list of for loop) is equal to the number of loops executed,  it
can be used to find the average ?. s divided by i will give the average .

My question is, the way i have used variable in the target list, just after
the completion of for loop, is its appropriate use or not.

Regards
Manprit Singh


More information about the Tutor mailing list