Python Loop statement
Function: Executes one or more statements repeatedly according to certain conditions
There are two types of looping 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 to determine if the truth expression is true
(2) If the 1th step is true, then EXECUTE statement 1 and skip to step 1th, or skip to the 3rd step;
(3) Execute ELSE clause
(4) ending execution of a while statement
while n <= : print(n )+ = 1
Exercise 1: Print the triangle with the string * operator (requires an integer that represents the distance of the triangle from the left-hand string *)
n = Int (input ("Please enter an integer:")) left=' '*NPrint(Left +' *')Print(Left +' ***')Print(Left +' *****')Print(Left +'*******')>>> ================================ RESTART ================================>>>Please enter an integer:3 * * * * * * * * * *******>>> ================================ RESTART ================================& Gt;>>Please enter an integer: 0* * * * ************>>>
Exercise 2 Enter three lines of text, let three lines of text in a box center display (input do not enter Chinese);
1 #!/usr/bin/python2 3 #enter 3 lines of text4line1 = input ("Please enter the first line of characters:")5Line2 = input ("Please enter the second line of characters:")6Line3 = input ("Please enter the third line of characters:")7 8 #compares the maximum line character length in 3 rows9m =Max (len (line1), Len (line2), Len (line3))Ten One #Center The text of 3 lines of output A Print('+'+'-'* (M + 2) +'+') - Print('| '+ Line1.center (m) +' |') - Print('| '+ Line2.center (m) +' |') the Print('| '+ Line3.center (m) +' |') - Print('+'+'-'* (M + 2) +'+') ->>> ================================ RESTART ================================ ->>> + Please enter the first line character: Aaaaaaaaaaaaa - Please enter the second line character: Aaaaaaaa + Please enter the third line character: Aaaaaaaaaa A+---------------+ at| aaaaaaaaaaaaa | -| aaaaaaaa | -| aaaaaaaaaa | -+---------------+ ->>>
While statement nesting
Grammar:
While a > B:
While B > C:
......
Else
.......
1>>> i = 12>>> whileI < 10:3j = 14 whileJ < 10:5 Print("i ="I"j =", J)6J + = 17i + = 18 9 Teni = 1 J = 1 Onei = 1 J = 2 Ai = 1 J = 3 -i = 1 J = 4 -i = 1 J = 5 thei = 1 J = 6 -i = 1 J = 7 -i = 1 J = 8 -i = 1 J = 9 +i = 2 J = 1 -i = 2 J = 2 +i = 2 J = 3 Ai = 2 J = 4 ati = 2 J = 5 -i = 2 J = 6 -i = 2 J = 7 -i = 2 J = 8 -i = 2 J = 9 -i = 3 J = 1 ini = 3 J = 2 -i = 3 J = 3 toi = 3 J = 4 +i = 3 J = 5 -i = 3 J = 6 thei = 3 J = 7 *i = 3 J = 8 $i = 3 J = 9Panax Notoginsengi = 4 J = 1 -i = 4 J = 2 thei = 4 J = 3 +i = 4 J = 4 Ai = 4 J = 5 thei = 4 J = 6 +i = 4 J = 7 -i = 4 J = 8 $i = 4 J = 9 $i = 5 J = 1 -i = 5 J = 2 -i = 5 J = 3 thei = 5 J = 4 -i = 5 J = 5Wuyii = 5 J = 6 thei = 5 J = 7 -i = 5 J = 8 Wui = 5 J = 9 -i = 6 J = 1 Abouti = 6 J = 2 $i = 6 J = 3 -i = 6 J = 4 -i = 6 J = 5 -i = 6 J = 6 Ai = 6 J = 7 +i = 6 J = 8 thei = 6 J = 9 -i = 7 J = 1 $i = 7 J = 2 thei = 7 J = 3 thei = 7 J = 4 thei = 7 J = 5 thei = 7 J = 6 -i = 7 J = 7 ini = 7 J = 8 thei = 7 J = 9 thei = 8 J = 1 Abouti = 8 J = 2 thei = 8 J = 3 thei = 8 J = 4 thei = 8 J = 5 +i = 8 J = 6 -i = 8 J = 7 thei = 8 J = 8Bayii = 8 J = 9 thei = 9 J = 1 thei = 9 J = 2 -i = 9 J = 3 -i = 9 J = 4 thei = 9 J = 5 thei = 9 J = 6 thei = 9 J = 7 thei = 9 J = 8 -i = 9 J = 9 the>>>
For Loop statement
A For statement can be used to traverse or iterate over each element of an object;
An iterative object includes:
String str, List of tuples, tuple tuples, dictionary dict, set set, fixed set Frozenset, iterator
Syntax for the FOR statement:
For variable list in iterate object
Statement 1
。。。。
Else
Statement 2
。。。。
Description: The ELSE clause can be omitted, and the number of executions of statement 1 is related to the number of elements of an iterative object;
#!/usr/bin/pythons="Hello" forIinchS:Print("i->>", i)Else: Print("For statement End")Print("End of program")>>> ================================ RESTART ================================>>>I->>Hi->>ei->>Li->>Li->>o forend of statement End program>>>
Exercise: Input a string, calculate the number of characters ' a ', and print it out;
The path to Python, fourth: Introduction to Python and Basics 5