# 如何在Python3中构造“for循环”

Published on November 14, 2019
By Lisa Tagliaferri
Developer and author at DigitalOcean.

## for循环

``````for [循环计数器] in [循环序列]:
[执行循环任务]
``````

[循环任务]在循环序列用尽之前，将会将一直被执行。

``````for i in range(0,5):
print(i)
``````

``````Output0
1
2
3
4
``````

## 用range()函数进行For循环

Python的内置类型中，有一个“不可变序列”是`range()`。在循环中，`range()`被用于控制循环重复的次数。

• `start` “起始值”决定这序列起始的数字，如果没有提供这个值，那么`start`将默认从0开始
• `stop` “终止值”是必需参数，它决定了循环在哪个数字终止(终止于到达这个数字前，因此不包含这个数字本身)
• `step` “步长”设定了数字在下一次迭代中“增加的幅度”（在负数的情况下，则为“减少的幅度”），如果没有提供这个参数则默认值为1

``````for i in range(6):
print(i)
``````

``````Output0
1
2
3
4
5
``````

``````for i in range(20,25):
print(i)
``````

``````Output20
21
22
23
24
``````

`range()`中的`step`参数类似于指定步幅进行字符串切割，可用于跳过序列中的某些值。

``````for i in range(0,15,3):
print(i)
``````

``````Output0
3
6
9
12
``````

``````for i in range(100,0,-10):
print(i)
``````

``````Output100
90
80
70
60
50
40
30
20
10
``````

## 使用“序列数据类型”的For循环

``````sharks = ['hammerhead', 'great white', 'dogfish', 'frilled', 'bullhead', 'requiem']

for shark in sharks:
print(shark)
``````

``````Outputhammerhead
great white
dogfish
frilled
requiem
``````

``````sharks = ['hammerhead', 'great white', 'dogfish', 'frilled', 'bullhead', 'requiem']

for item in range(len(sharks)):
sharks.append('shark')

print(sharks)
``````
``````Output['hammerhead', 'great white', 'dogfish', 'frilled', 'bullhead', 'requiem', 'shark', 'shark', 'shark', 'shark', 'shark', 'shark']
``````

``````integers = []

for i in range(10):
integers.append(i)

print(integers)
``````

``````Output[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
``````

``````sammy = 'Sammy'

for letter in sammy:
print(letter)
``````
``````OutputS
a
m
m
y
``````

``````sammy_shark = {'name': 'Sammy', 'animal': 'shark', 'color': 'blue', 'location': 'ocean'}

for key in sammy_shark:
print(key + ': ' + sammy_shark[key])
``````
``````Outputname: Sammy
animal: shark
location: ocean
color: blue
``````

## 嵌套循环

``````for [first iterating variable] in [outer loop]: # Outer loop
[do something]  # Optional
for [second iterating variable] in [nested loop]:   # Nested loop
[do something]
``````

``````num_list = [1, 2, 3]
alpha_list = ['a', 'b', 'c']

for number in num_list:
print(number)
for letter in alpha_list:
print(letter)
``````

``````Output1
a
b
c
2
a
b
c
3
a
b
c
``````

``````list_of_lists = [['hammerhead', 'great white', 'dogfish'],[0, 1, 2],[9.9, 8.8, 7.7]]

for list in list_of_lists:
print(list)
``````
``````Output['hammerhead', 'great white', 'dogfish']
[0, 1, 2]
[9.9, 8.8, 7.7]
``````

``````list_of_lists = [['hammerhead', 'great white', 'dogfish'],[0, 1, 2],[9.9, 8.8, 7.7]]

for list in list_of_lists:
for item in list:
print(item)
``````
``````Outputhammerhead
great white
dogfish
0
1
2
9.9
8.8
7.7
``````

## 总结

Developer and author at DigitalOcean.

Gongxia Chen

translator

Developer and author at DigitalOcean.

#### Still looking for an answer?

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!