Loop statement: While loop, for loop
Example 1: The and of all numbers of 1-100
n = + = 1 while count <= N: = sum + count + = 1 print ( c8> '1 to%d sum:%d' % (n,sum))
Output Result:
1 to 100 of the sum of: 5050
Example 2:for cycle
A = ['what','where',' How',' Why' " in range (len (a)): Print(I,a[i])
Output Result:
0What 1 where2 how 3 why
Example 3:
forLetterinch 'Runoob': ifLetter = ='b': Break #When the letter B appears, exit the loop! Print('The current letter is:', letter) var= 10 whilevar >0:Print('the current variable value is:', Var) var= Var-1ifvar = = 5#Value is 5 o'clock, exit loop BreakPrint('Goodbye!')
Output Result:
9876Goodbye!
Example 4:
for inch (0,10,3) # Press 3 to increase from 0 to 10 Print (i)
The output is:
0369
Example 5: Finding odd even numbers
# Ask 100 inside odd number: for in range: if i% 2 = = 1 :print(i)# ask for an even number within 100: for in range: if i% 2 = = 0: print(i)
Python: Looping