First, the circulation
1. For
names = ['Michal','Bob','Tracy'] forNameinchnames:Print(name) sum=0 forXinch[1, 2, 3, 4, 5, 6, 7,8,9,10]: Sum= Sum +xPrint(sum)#Print Digital 0-9 forXinchRange (10): Print(x)
2. While
sum =0n= 99 whilen >0:sum= Sum +N N= N-2Print(SUM) n= 1 whileN <= 100: ifn > 10:#when n = 11 o'clock, the condition is met, the break statement is executed Break #The break statement ends the current loop Print(n) n= n + 1Print("End") n=0 whileN < 10: N= n + 1ifn% 2 = = 0:#if n is an even number, execute the Continue statement Continue #The continue statement will proceed directly to the next loop, and subsequent print () statements will not execute Print(n)#Calculate 1+2+3+...+100:sum =0n= 1 whileN <= 100: Sum= Sum +N N= n + 1Print(sum)#Calculate 1x2x3x...x100:ACC = 1N= 1 whileN <= 100: ACC= ACC *N N= n + 1Print(ACC)
Xuefeng Liao Website: Learning Python Basics-Loops (iv)