python for 循环写法

python for 循环写法


2024年5月22日发(作者:)

python for 循环写法

Python中的for循环有两种写法:

1. 遍历序列

```python

# 遍历列表

lst = [1, 2, 3, 4, 5]

for i in lst:

print(i)

# 遍历字符串

s = "hello"

for c in s:

print(c)

# 遍历元组

t = (1, 2, 3)

for i in t:

print(i)

# 遍历集合

s = {1, 2, 3}

for i in s:

print(i)

# 遍历字典

d = {"a": 1, "b": 2, "c": 3}

for k, v in ():

print(k, v)

2. 循环固定次数

```python

# 循环10次

for i in range(10):

print(i)

# 循环5次,从1开始

for i in range(1, 6):

print(i)

# 循环10次,步长为2

for i in range(0, 10, 2):

print(i)

```"


发布者:admin,转转请注明出处:http://www.yc00.com/web/1716327666a2727325.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信