For loops that counts the number of values in list


Wed Sep 21 2022 07:04:10 GMT+0000 (UTC)

Saved by @L0uJ1rky45M #python

count = 0 #Set the variable count to zero before the loop starts
for i in [3, 41, 12, 9, 74, 15]: # Our iteration variable, itervar, controls the loop and cause the loop body to be executed once for each values in list.
    count = count + 1  #In the body of the loop, we add 1 to the current value of count
print('Count: ', count)
# Once the loop completes, the value of count is the total number of items

This code shows that for loop can be used to count the number of values in a list. HOWEVER, making the counting loop above is not useful because there is a built-in function len( ).