The third chapter Python
of control statements
Control statements constituted by the conditional statements and loops, mainly if
, while
and for
.
3.1 Structured Programming
For the specific implementation of the program need to write to complete the structure, the structure is divided into three kinds of programming structures - sequential structure, determining the structure, a cyclic structure
3.2 conditional statement
if
Statement is used to detect a condition is satisfied. If true, then the implementation of if
the program in the statement; otherwise skip if
statements executed behind the content. Format is as follows:
if (表达式):
语句1
else:
语句2
input()
For capturing a user's original input, and converts it to a string.
input([prompt])
Parameter prompt
is output from the console question, prompts the user, the return value is a string. If you enter a number, you can call int()
convert it.
x = input("x: ")
x = int(x)
print x = x + 1
if...elif...else
Statements are if...else...
complementary statements, many branches when the program is in use. if
You can use nested statements, but not recommended to use, easy to read the program.
3.3 Loops
Loop refers to repeatedly executed the same block of code, typically set for traversing or cumulative. Python
The loop has while
statements and for
looping statements.
while
During the execution of the cycle: When the loop expression is true, followed by the implementation while
of the statement. Until the value of an expression False
, the program flow to else
the statement. In the python 3
middle, does not mean only !=
. When using the loop, it should be noted Boolean value of an expression to avoid infinite loop.
for
Cycle for traversing a collection in order to access the collection of each project.
for 变量 in 集合:
...
else:
...
for...in
During the execution of the cycle: each cycle retrieves a value from the set, and the value assigned to the variable. May be set tuples, lists, dictionaries and other data structures. for
Circulation else
clause also part of the cycle of the last cycle ended after use else
statement. for
And the cycle is often range()
used together function range()
returns a list.
range(start, stop[, step])
range()
Function returns anrange
object list of elements is determined by three parameters; parameterstart
value representing the beginning of the list, the default value is 0; parameterstop
value indicating the end of the list, the parameter indispensable; parameterstep
represents a step size, each increment or decrement of , default is 1.
break
Statement can make the program out of the loop, so that the program execution outside the loop, meaning break
statements can advance out of the loop, but the break
statement can only be placed in circulation; continue
the statement is also used to jump out of the cycle of statements, but it does not jump out of the whole cycle body, just out of the current cycle, and then continue the cycle back.
The chapter has an exercise, very interesting. It is used python
to calculate personal income tax related content.