For loop
It is a common method for iterating the elements of
an object. Any object with an iterable method can be used in a for loop.
A unique feature of python is that the code block is not surrounded by {} or begin, end.
In contrast, Python uses indentation, and the lines within the block must be indented by tabs, or 4 spaces relative to the surrounding commands.
for number in [23, 41, 12, 16, 7]:
print(number)
print('Hi')
Results:
1. Enumeration The
enumeration function returns a tuple containing the count of each iteration (starting from the default of 0) and the value obtained by the iteration sequence
friends = ['steve', 'rachel', 'michael', 'adam', 'monica']
for index, friend in enumerate(friends):
print(index,friend)
Result:
Practice a question:
#将这段话的标点符号去掉,并且把它分开到列表中
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 a in ['-',',',';','\"']:
text=text.replace(a,' ')#用空格代替
print(text)
text=text.split(' ')[0:100]#用空格给分开
print(text)
Result:
2. len('')
#将这段话的标点符号去掉,并且把它分开到列表中
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 a in ['-',',',';','\"']:
text=text.replace(a,' ')#用空格代替
list=[]
for a in text.split(' '):
if len(a)>0:#如果提取出来的长度大于0就放进列表中
list.append(a)
print(list[0:20])
Result:
3. Continue The
continue statement will go to the next iteration of the loop. The
continue statement is used to ignore certain values, but will not interrupt the loop.
for a in ['-',',',';','\"']:
text=text.replace(a,' ')#用空格代替
list=[]
for a in text.split(' '):
if len(a)==0:#如果提取出来的=0就下一次循环
continue
list.append(a)
print(list[0:20])
Result:
4. Break
break statement will completely interrupt the loop
#将这段话的标点符号去掉,并且把它分开到列表中
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 a in ['-',',',';','\"']:
text=text.replace(a,' ')#用空格代替
list=[]
for a in text.split(' '):
if len(a)==0:
break
list.append(a)
print(list[0:20])
result:
Exercise:
Write a Python program that iterates integers from 1 to 50 (using a for loop). For even integers, append them to the list even_numbers. For odd integers, append them to the odd odd list.
Reminder: range(1,51) represents an integer from 1 to 50
even_number=[]#偶数列表
odd_number=[]#奇数列表
for a in range(1,51):
if a%2==0:
even_number.append(a)
else:
odd_number.append(a)
print(even_number)
print(odd_number)
result: