January 2019
Beginner
318 pages
8h 23m
English
for loop iterates over each item of the sequence or any other iterable object and it will execute the statements in the for block each time. Refer to the following syntax:
for i in sequence: for loop body
Here, i is the variable that takes the value of the item inside the sequence on each iteration. This loop continues until we reach the last item in the sequence. This is illustrated in the following diagram:

Refer to the following example:
numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11]sum = 0for i in numbers: sum = sum + i print("The sum is", sum)Output:The sum is 6The sum is 11The sum is 14The sum is 22The sum is 26The sum is 28The sum ...Read now
Unlock full access