# 迭代列表for number in[23,41,12,16,7]:print(number)'''
23
41
12
16
7
'''
02、枚举
返回一个元组,其中包含每次迭代的计数(从默认为0开始)和迭代序列获得的值
# 迭代列表
lists =['steve','rachel','michael','adam','monica']for index,listinenumerate(lists):print(index,list)'''
0 steve
1 rachel
2 michael
3 adam
4 monica
'''
03、Continue
continue语句将转到循环的下一次迭代
continue语句用于忽略某些值,但不会中断循环
continue的使用在下面的示例中
04、Break
break语句将完全打断循环
# 申明一个列表
cleaned_list =[]# 定义文本字符串
x ="This is a example break, look quickly!"# 用空格替换标点符号for char in',!':
x = x.replace(char,' ')for word in x.split(' '):if word =='look':break
cleaned_list.append(word)print(cleaned_list)# ['This', 'is', 'a', 'example', 'break', '']
05、示例
从文本中删除标点符号并将最终结果转换为列表
# 字符串的声明
text ='''On a dark desert highway, cool wind in my hair Warm smell of colitas, rising up through the air Up ahead in the distance, I saw a shimmering light My head grew heavy and my sight grew dim I had to stop for the night There she stood in the doorway; I heard the mission bell And I was thinking to myself, "This could be Heaven or this could be Hell" Then she lit up a candle and she showed me the way'''
# 将文本中的标点符号之类的,用空格来代替--for循环for char in'-.,;\"':
text = text.replace(char,' ')print(text)