Fortieth day of learning python - python control statements Chapter III

The third chapter Pythonof control statements

Control statements constituted by the conditional statements and loops, mainly if, whileand 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

ifStatement is used to detect a condition is satisfied. If true, then the implementation of ifthe program in the statement; otherwise skip ifstatements 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 promptis 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...elseStatements are if...else...complementary statements, many branches when the program is in use. ifYou 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. PythonThe loop has whilestatements and forlooping statements.

whileDuring the execution of the cycle: When the loop expression is true, followed by the implementation whileof the statement. Until the value of an expression False, the program flow to elsethe statement. In the python 3middle, does not mean only !=. When using the loop, it should be noted Boolean value of an expression to avoid infinite loop.

forCycle for traversing a collection in order to access the collection of each project.

for 变量 in 集合:
    ...
else:
    ...

for...inDuring 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. forCirculation elseclause also part of the cycle of the last cycle ended after use elsestatement. forAnd the cycle is often range()used together function range()returns a list.

range(start, stop[, step])

range()Function returns an rangeobject list of elements is determined by three parameters; parameter startvalue representing the beginning of the list, the default value is 0; parameter stopvalue indicating the end of the list, the parameter indispensable; parameter steprepresents a step size, each increment or decrement of , default is 1.

breakStatement can make the program out of the loop, so that the program execution outside the loop, meaning breakstatements can advance out of the loop, but the breakstatement can only be placed in circulation; continuethe 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 pythonto calculate personal income tax related content.

Published 72 original articles · won praise 42 · views 390 000 +

Guess you like

Origin blog.csdn.net/A_lPha/article/details/53739252