- 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
推薦閱讀
- Spring Cloud Alibaba核心技術與實戰案例
- 精通Nginx(第2版)
- Learning Spring 5.0
- Android 9 Development Cookbook(Third Edition)
- Linux網絡程序設計:基于龍芯平臺
- Securing WebLogic Server 12c
- 微信小程序開發解析
- 機器學習與R語言實戰
- jQuery Mobile移動應用開發實戰(第3版)
- MySQL從入門到精通(軟件開發視頻大講堂)
- Spring Boot實戰
- C語言程序設計與應用(第2版)
- Android系統下Java編程詳解
- Mastering Concurrency in Python
- ROS機器人編程實戰