Python之for循环

来源:互联网 发布:二代防火墙 知乎 编辑:程序博客网 时间:2024/05/23 21:15
#Python的for可以遍历  a list  or  a string#Measure  some stringswords = ['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']"""