网站制作学习网Python→正文:python for循环
字体:

python for循环

Python 2023/4/11 11:11:36  点击:不统计


原文是网站制作学习网的FoAsP.cn
 Python中的for循环是一种迭代器,用于遍历一个序列或集合中的元素。以下是一个简单的for循环的例子

# 遍历一个列表
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
 print(fruit)

# 遍历一个字符串
for char in "Hello":
print(char)

# 遍历一个元组
for (index, value) in enumerate((1, 2, 3)):
print(index, value)

# 遍历一个集合
for item in [1, 2, 3]:
print(item)

在这个例子中,我们使用for循环遍历了一个列表、一个字符串、一个元组和一个集合。在每个循环迭代中,我们使用了不同的变量来访问序列、字符串、元组和集合中的元素。
原载于:文章来源:www.forasp.cn网站制作学习
转www.载for网站制作学习asp必.cn究

·上一篇:cannot import name 'unicode'/'int' from 'numpy' >>    ·下一篇:python 实现哈希sha1加密 >>
推荐文章
最新文章