Python 3 Study Notes: Process Control

Program Structure

Computer in solving a specific problem, there are three forms, which are all statements control the execution order, select the operative part of the statement and the loop execution of the statement. The form of the above three problem solving program design process, there are three basic configurations, namely, sequential structure, and selecting structure of the cyclic structure.

Sequence control

Order structure is well understood, is the computer program in accordance with the order of statements executed sequentially from top to bottom, every statement will be faithfully executed, so there is a lot of limitations. If a problem occurs there may be many, but the actual occurrence of monolingual, sequential structure can not do this kind of choice.

Select Control

Satisfies the condition that the implementation is not satisfied is skipped.

if …

if the English words in Chinese means "if" in the programming, that is if ... then ...,

. 1
2
IF expression:
do some Things
replication
expression is an expression, the result of the expression is true (True), is performed do somthing; if false (False), then do something skipped block.

if … else …

After the if statement should be given to meet the conditions to do, but did not give a condition is not fulfilled what should be done. Therefore, it is derived from the else statement,

1
2
3
4
IF expression:
do some Things
the else:
do some Things
copy
if ... else ... statement gives the false (False) if expression is the result of expression, the program should do something.

if … elif … else…

If there is a problem many possible outcomes to choose from, we need to use elif statement,

1
2
3
4
5
6
7
IF expression The 1:
do some Things
elif expression The 2:
do some Things
...
the else:
do some Things
copy
of the statement to choose from a number of possible options, that is, if the first expression style to meet the conditions, the subsequent statement will be executed, and then jump out of the entire structure, regardless of whether elif expression is true (even after the elif expressions are true, the subsequent statement will not be executed). Only in front of all expressions are false, will execute the statement after else.

Loop control

If the condition goes to the loop, the loop is finished, the re-determination condition is satisfied, the process proceeds to the loop if they meet again, after the statement is executed in the loop body and vice versa.

while

while loop is controlled by a condition of whether the loop is repeatedly performed,

1
2
the while expression:
do some Things
replication
expression for the first time is true, the loop body is executed, the calculation expression again after finished, if it is true then the loop body is executed again; if false, the statement after the loop body is executed.

for

a for loop is executed sequentially from the round robin, or commonly used in the enumeration traversal sequence, and an iterative object elements,

. 1
2
for objects in iteration:
do some Things
replication
iteration is one that can be removed from the objects iteration variable, i.e., as long as the objects present in the iteration, the loop is executed.

For example, we want to calculate the cumulative sum of 1 to 100, can be used for loop,

1
2
3
4
5
6
result = 0

for number in range(1, 101):
result += number

print (result)
Copy
wherein, Range () function to generate a series of consecutive integers within a certain range,

. 1
Range (start, End, STEP)
copy
start to the start values, the default value is 0; end to end of the specified value; STEP specifies the step size, i.e., the interval (between two consecutive number which is the difference in absolute value), the default value is 1.

In Python programming, like range () in this class has a function of starting and ending worth, which is the starting value in the range <= x <end value

Of course, the above statement, we can also use the while statement to achieve,

1
2
3
4
5
6
7
8
result = 0
number = 1

while number < 101:
result += number
number += 1

print (result)
copy
for the statement also been traversal sequence,

1
2
3
4
list = ["hello", "python", 2019, 8, 1]

List Item in for:
Print (Item, End = "\ T")
Copy
Nested

In a selection (cycle) control statements, there are a plurality of selection (cycle) control statements, it is referred to as selection (cycle) nested statements, nested theoretically unlimited.

1
2
3
4
for i in range(1, 10):
for j in range(1, i+1):
print(str(j) + "x" + str(i) + "=" + str(j * i) + "\t", end="")
print()
复制
跳转语句

break

The break statement can terminate the current cycle, generally used in combination with the use of an if statement that out of the current loop when certain conditions are met, continue with the statement following the loop.

1
2
3
4
5
while expression:
do some things

if condition:
    break

Copy
or,

1
2
3
4
5
for iteration in objects:
do some things

if condition:
    break

Copy
continue

continue statement can not be the end of the cycle, only to jump out of this cycle, the next cycle execution in advance,

1
2
3
4
5
while expression:
do some things

if  condition:
    continue

Copy
or,

1
2
3
4
5
for iteration in objects:
do some things

if  condition:
    continue

Copy
pass sentence

pass statement has no practical effect, occupying only play the role. For example, if the structure, if the conditions are met on the implementation of a system statement, but when has not figured out how to write these statements, you can use the pass placeholder, to ensure the normal operation of the program can be down, to be replaced at the thought of good pass can be.

Guess you like

Origin blog.51cto.com/14499640/2429607