3rd Chapter Python's control statement
-If statement
-If ... else ... Statement
-If ... elif ... else ... Statement
-While Loop
-For Loop
-Break statement
-Structured programming
Input ([prompt])--> numbers, receiving numbers or expressions
Raw_input ([Prompt])--> string, receiving arbitrary content
3.1 Structured Programming
Structured programming is centered on the design of modules,
The software system to be developed is divided into several modules which are independent of each other.
This makes the work of completing each module clear,
For the design of some large software to lay a good foundation.
The main method of structured programming is to---top-down, and gradually refine
Structured programming is divided into 3 kinds of structure---sequential judgment cycle
3.2 -piece statementConditional statements are calculated based on the different results of a conditional expression,
Causes the program to execute to a different code block.
3.2.1 If statement
if (expression):
Statement 1
[
else:
Statement 2
]
3.2.2 If elif Else statement
if (expression 1):
Statement 1
elif (expression 2):
Statement 2
[
else:
Statement 3
]
3.3 Circular Statements-While
-For
A circular statement is a repeated execution of the same piece of code.
The circular statement is composed of two parts, the circulation body and the termination condition.
Loop body: A group of statements that are repeatedly executed
Termination condition: Number of times to control repeat execution
3.3.1 While Loop
while (expression):
...
[
else:
...
]
The process of the program is transferred to the Else statement until the value of the loop expression is False
That is, the while loop executes normally before the Else statement is executed
3.3.2 for Loop
For variable in collection:
...
[
else:
...
]
Used to traverse a collection and then access each item in the collection in turn.
Execution procedure: Each loop takes a value from the collection and assigns the value to the variable
Variables need not be declared separately
Collections can be data structures such as tuple list dictionaries
Lists can be generated by range ([Start,]stop[,step])
The list can also be generated by xrange ([Start,]stop[,step])
Range returns the entire list at once
Xrange returns a value each time, the resource consumption is small, the efficiency is high
3.3.3 Break and Continue
To control the jump of a statement
-Break: End the entire loop
-Continue: End of Time cycle
3.4 Bubbling Program
def bubblesort (Numbers): For x in xrange (len (Numbers)-1,-1,-1): For y in Xrange (x): if (numbers[y) > Numbers[y + 1]): Numbers[y], numbers[y + 1] = numbers[y + 1], Numbers[y] Print numbers def bubblesort_2 (num bers): For x in xrange (0, Len (numbers)-1, 1): For y in xrange (0, Len (numbers)-x-1, 1): if (numbers[y ] > numbers[y + 1]): Numbers[y], numbers[y + 1] = numbers[y + 1], numbers[y] Print numbers "" for (in t i = 0; i < arr.length-1;
i++) {for (int j = 0; J < arr.length-i-1; j +) if (Arr[j] > arr[j+1]) {int temp = Arr[i];
Arr[i] = Arr[j];
ARR[J] = temp; for (int i = 0; i < arr.length-1. i++) {for (int j = 0; J < arr.length-i-1; + +) if (Arr[j] >
Arr[j+1]) {int temp = Arr[i];
Arr[i] = Arr[j];
ARR[J] = temp; } "" "Def Main (): numbers = [7, 2, 3, 5, 8, 1, 9, 4, 6, 0] Bubblesort (numbers) numbers_2 = [7, 2, 3, 5, 8, 1, 9,4, 6, 0] bubblesort_2 (numbers_2) If __name__ = ' __main__ ': Main () # Print range (9,-1,-1) # Print range (0, 10, 1) # Print range (0) Pass