Home > Article > Backend Development > python basics-loop
for loop:
a = [11,22,33,44]
for i in a:
print (i)
result:
11
22
33
44
while loop:
i=0
while i < 3: #i<3 is the loop condition. When the condition is true, the following loop body will be executed; when the condition is false, it will not be executed.
print (i)
i += 1
Result:
0
1
2
break and Continue:
i= 0
while i < 10:
print ("i->:",i)
if i == 5:
break #When i==5, exit all loops
i += 1
Result:
i->: 0
i->: 1
i->: 2
i->: 3
i->: 4
i->: 5
x=0
while x < 9:
for x in range(0,10):
if x ==6:
continue #when x== At 6 o'clock, jump out of the current loop and continue to the next loop
Jump out of the current loop and continue to the next cycle. 2
x->: 3
x->: 5
x->: 7 #missingx->: 6x->: 8
x->: 9
Others:
enumrate:
a = ["aa","bb","cc","dd"]
Print (i)
Result:
(5, 'aa')
(6, 'bb')
(7, 'cc')
(8, 'dd')
1,
for n in range(1, 10):
print (n)
2
3
4
6
78
9
2 ,
for x in range(0,20,4):
print (x)
4
8
12
#Use 4 as step and print