Four, python process operation

Select condition

Single branch

if 条件:
    满足条件执行的代码

Dual Branch

if 条件:
    满足条件执行代码
else
    不满足条件执行语句

Multi-Branch

if 条件1:
    满足条件1执行代码
elif 条件2:
    条件1不满足但条件2满足执行的代码
else:
    上面的条件都不满足执行的代码

Execute the statement indentation which is a major feature of Python, forcing indentation, the purpose is to let the program know that each piece of code that depends on what conditions.

Second, the operation cycle

while loop

For we do not know the case of multiple cycles

while 条件: 
    循环体代码

If the condition is true, then enters a loop the loop body execute code if the condition is false, then the cycle ends.

while True The infinite loop

while结合else
else while the latter effect means that when the while loop is normally been executed (intermediate not been suspended break), executes the behind else

while count <= 5 :
    count += 1
    print("Loop",count)

else:
    print("循环正常执行完啦")

for loop

When used for or know the number of cycles of use iterables

for iterating_var in sequence:
   循环体代码

for结合else
Means for acting when a loop statement normally been executed (with no break is suspended), executes the behind else

Off-cycle

continue and break somewhat similar, except that only continue to terminate the present cycle, then the next cycle is also performed, completely break the whole cycle terminates

Nested loop
Python language allows a circulation loop embedded inside another.

Python for loop nested syntax:

for iterating_var in sequence:
   for iterating_var in sequence:
      statements(s)
   statements(s)

Python while loop nested syntax:

while expression:
   while expression:
      statement(s)
   statement(s)

Published 40 original articles · won praise 2 · Views 2069

Guess you like

Origin blog.csdn.net/weixin_42155272/article/details/93856804