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

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
主站蜘蛛池模板: 班玛县| 东台市| 蓝山县| 志丹县| 元阳县| 延吉市| 房产| 延津县| 丰镇市| 临朐县| 镇安县| 建瓯市| 读书| 溧阳市| 正安县| 太仆寺旗| 绥芬河市| 微博| 桦南县| 蓬安县| 浪卡子县| 桐城市| 仙游县| 筠连县| 日喀则市| 合水县| 北辰区| 尼勒克县| 承德县| 奎屯市| 砚山县| 荔波县| 肃宁县| 西盟| 南开区| 绥江县| 衡南县| 宜丰县| 兴城市| 栾城县| 彝良县|