- Mastering Python Scripting for System Administrators
- Ganesh Sanjiv Naik
- 146字
- 2021-07-02 14:00:28
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
推薦閱讀
- Learn ECMAScript(Second Edition)
- 新編Visual Basic程序設(shè)計(jì)上機(jī)實(shí)驗(yàn)教程
- 程序員面試白皮書
- Spring 5.0 By Example
- Python貝葉斯分析(第2版)
- Python數(shù)據(jù)可視化之Matplotlib與Pyecharts實(shí)戰(zhàn)
- 名師講壇:Spring實(shí)戰(zhàn)開發(fā)(Redis+SpringDataJPA+SpringMVC+SpringSecurity)
- 飛槳PaddlePaddle深度學(xué)習(xí)實(shí)戰(zhàn)
- Red Hat Enterprise Linux Troubleshooting Guide
- ASP.NET Web API Security Essentials
- Mudbox 2013 Cookbook
- 30天學(xué)通C#項(xiàng)目案例開發(fā)
- Android高級(jí)開發(fā)實(shí)戰(zhàn):UI、NDK與安全
- 高性能MVVM框架的設(shè)計(jì)與實(shí)現(xiàn):San
- Elasticsearch實(shí)戰(zhàn)(第2版)