python loop

 

while loop

# print 1 to 10 
num = 0
while num < 10: the while syntax is the same as if
num += 1 is a limit, every time num is increased by 1, otherwise it will enter an infinite loop
print(num)

 

 

# print 1 to 10 and add 
num = 0
num1 = 0
while num < 10:
num += 1
num1 += num num1 is equivalent to adding up the num results of each loop
print(num,num1)

 

num = 0 
while num < 10:
num += 1
print("num:",num)
else: means
print("the loop program has been executed") after the above loop ends

 

for loop (traverse a collection)

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325170205&siteId=291194637
Recommended