官术网_书友最值得收藏!

for loop

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 = 0
for i in numbers:
sum = sum + i
print("The sum is", sum)

Output:
The sum is 6
The sum is 11
The sum is 14
The sum is 22
The sum is 26
The sum is 28
The sum is 33
The sum is 37
The sum is 48
主站蜘蛛池模板: 科尔| 长治市| 穆棱市| 洛隆县| 安化县| 全南县| 永年县| 紫云| 洛南县| 广水市| 辽中县| 阳高县| 耒阳市| 西丰县| 兰溪市| 调兵山市| 连平县| SHOW| 襄汾县| 永修县| 茶陵县| 哈巴河县| 长治市| 顺义区| 宁海县| 东阿县| 大冶市| 思南县| 略阳县| 宁陵县| 恩施市| 阜宁县| 绥中县| 布尔津县| 清涧县| 林州市| 星子县| 饶河县| 浮山县| 正安县| 丽水市|