Python新手指南:如何使用for循环遍历序列
在Python中,for循环是一种常见的迭代工具,用于遍历各种序列类型。下面是一个基本的for循环遍历序列的例子:
列表(List):
fruits = ['apple', 'banana', 'orange']
for fruit in fruits:
print(fruit)
输出:
apple
banana
orange
字符串(String):
sentence = "Hello, world!"
for char in sentence:
print(char)
输出:
```
H
e
l
l
,
w
o
r
l
d
!
3. **元组(Tuple)**:
```python
colors = ('red', 'green', 'blue'))
for color in colors:
print(color)
输出:
red
green
blue
以上就是Python中使用for循环遍历序列的基本操作。
还没有评论,来说两句吧...