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条)