#Python的for可以遍历 a list or a string
#Measure some strings
words = ['happy', 'smile', 'sunshine']
for word in words :
print(word, len(word))
"""
result:
happy 5
smile 5
sunshine 8
"""
"""
在for循环遍历的时候,可以加入条件遍历,也可以随即改变集合,比如向集合中copy元素,
words[:]创建一个无限长度的集合words
"""
for word in words[:]:
if len(word) >6 :
words.insert(0,word)
print(words)
"""
result:
['sunshine', 'happy', 'smile', 'sunshine']
"""
Python之for循环
最新推荐文章于 2023-06-12 09:14:09 发布