python loop statement
Function: Repeatedly execute one or more statements according to certain conditions
Two loop statements:
while statement
for statement
while statement:
grammar:
while truth expression:
Statement 1
。。。
else:
Statement 2
。。。
Syntax description: else clause can be omitted
Execution order: (1) First determine whether the truth expression is True
(2) If step 1 is True, execute statement 1 and skip to step 1, otherwise skip to step 3;
(3) Execute the else clause
(4) End the execution of the while statement
>>> n = 1 >>> while n <= 10 : print(n) n += 1 1 2 3 4 5 6 7 8 9 10 >>>
Exercise 1: Print the triangle with the string * operator; (requires an integer representing the distance of the triangle from the string * on the left)
n = int(input( " Please enter an integer: " )) left = ' ' * n print(left + ' *') print(left + ' ***') print(left + ' *****') print(left + '*******') >>> ================================ RESTART ================================ >>> Please enter an integer: 3 * *** ***** ******* >>> ================================ RESTART ================================ >>> Please enter an integer: 0 * *** ***** ******* >>>
Exercise 2 Enter three lines of text, and let the three lines of text be centered in a box (do not enter Chinese);
1 # !/usr/bin/python 2 3 #Enter 3 lines of text 4 line1 = input( " Please enter the first line of characters: " ) 5 line2 = input( " Please enter the second line of characters: " ) 6 line3 = input ( " Please enter the third line of characters: " ) 7 8 #Compare the length of the largest line of characters in the 3 lines 9 m = max(len(line1), len(line2), len(line3)) 10 11 #Make 3 lines Center the output text 12 print ( ' + ' + ' - ' * ( m + 2 ) + '+') 13 print('| ' + line1.center(m) + ' |') 14 print('| ' + line2.center(m) + ' |') 15 print('| ' + line3.center(m) + ' |') 16 print('+' + '-' * ( m + 2 ) + '+') 17>>> ================================= RESTART =============== ================= 18 >>> 19 Please enter the first line of characters: aaaaaaaaaaaaa 20 Please enter the second line of characters: aaaaaaaa 21 Please enter the third line of characters: aaaaaaaaaa 22 +---------------+ 23 | aaaaaaaaaaaaa | 24 | aaaaaaaa | 25 | aaaaaaaaaa | 26 +---------------+ 27 >> >
while statement nesting
grammar:
while a > b:
while b > c:
......
else:
.......
1 >>> i = 1 2 >>> while i < 10: 3 j = 1 4 while j < 10: 5 print("i =", i, "j =", j) 6 j += 1 7 i += 1 8 9 10 i = 1 j = 1 11 i = 1 j = 2 12 i = 1 j = 3 13 i = 1 j = 4 14 i = 1 j = 5 15 i = 1 j = 6 16 i = 1 j = 7 17 i = 1 j = 8 18 i = 1 j = 9 19 i = 2 j = 1 20 i = 2 j = 2 21 i = 2 j = 3 22 i = 2 j = 4 23 i = 2 j = 5 24 i = 2 j = 6 25 i = 2 j = 7 26 i = 2 j = 8 27 i = 2 j = 9 28 i = 3 j = 1 29 i = 3 j = 2 30 i = 3 j = 3 31 i = 3 j = 4 32 i = 3 j = 5 33 i = 3 j = 6 34 i = 3 j = 7 35 i = 3 j = 8 36 i = 3 j = 9 37 i = 4 j = 1 38 i = 4 j = 2 39 i = 4 j = 3 40 i = 4 j = 4 41 i = 4 j = 5 42 i = 4 j = 6 43 i = 4 j = 7 44 i = 4 j = 8 45 i = 4 j = 9 46 i = 5 j = 1 47 i = 5 j = 2 48 i = 5 j = 3 49 i = 5 j = 4 50 i = 5 j = 5 51 i = 5 j = 6 52 i = 5 j = 7 53 i = 5 j = 8 54 i = 5 j = 9 55 i = 6 j = 1 56 i = 6 j = 2 57 i = 6 j = 3 58 i = 6 j = 4 59 i = 6 j = 5 60 i = 6 j = 6 61 i = 6 j = 7 62 i = 6 j = 8 63 i = 6 j = 9 64 i = 7 j = 1 65 i = 7 j = 2 66 i = 7 j = 3 67 i = 7 j = 4 68 i = 7 j = 5 69 i = 7 j = 6 70 i = 7 j = 7 71 i = 7 j = 8 72 i = 7 j = 9 73 i = 8 j = 1 74 i = 8 j = 2 75 i = 8 j = 3 76 i = 8 j = 4 77 i = 8 j = 5 78 i = 8 j = 6 79 i = 8 j = 7 80 i = 8 j = 8 81 i = 8 j = 9 82 i = 9 j = 1 83 i = 9 j = 2 84 i = 9 j = 3 85 i = 9 j = 4 86 i = 9 j = 5 87 i = 9 j = 6 88 i = 9 j = 7 89 i = 9 j = 8 90 i = 9 j = 9 91 >>>
for loop statement
The for statement can be used to traverse or iterable each element of an object;
Iterable objects include:
string str , list list , tuple tuple , dictionary dict , set set , fixed set frozenset , iterator
The syntax of the for statement:
for variable list in iterable object
Statement 1
。。。。
else:
Statement 2
。。。。
Note: The else clause part can be omitted; The number of executions of statement 1 is related to the number of elements of the iterable object;