python中for循环例子

python中for循环例子


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

python中for循环例子

Python中的for循环是一种常用的循环结构,用于遍历一个可迭代

对象(如列表、元组、字符串等)中的每个元素,并执行指定的操

作。下面将列举10个符合要求的for循环的例子。

1. 遍历列表中的元素

```python

fruits = ['apple', 'banana', 'orange']

for fruit in fruits:

print(fruit)

```

输出结果:

```

apple

banana

orange

```

2. 遍历元组中的元素

```python

colors = ('red', 'green', 'blue')

for color in colors:

print(color)

```

输出结果:

```

red

green

blue

```

3. 遍历字符串中的字符

```python

message = "Hello, world!"

for char in message:

print(char)

```

输出结果:

```

H

e

l

l

,

w


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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信