Cycle:
The execution of a piece of code repeats n times until a certain condition is met.
In order to stop repeating execution at the appropriate time, the program needs to have a condition that satisfies the stop loop.
There are three loops in Python (only two of them are in essence):
While loop
For loop
Nested loops
Break #完全结束一个循环本身
Continue #停止当前循环体的执行 (equivalent to ignoring the statement behind continue) to begin the next loop body;
While Loop #表达式为true, the loop body is executed, and the number of expressions that return a Boolean value is always evaluated more than the loop body.
Format:
While returns an expression of a Boolean value:
Statements to be executed
---------------------------------
#coding =utf8
num = 0
while (Num < 8):
Print num
num = num + 1
print ' over '
---------------------------------
#coding =utf8
#遇到2中断整个循环
num = 0
while (Num < 8):
num = num + 1
if num = = 2:
Break
Print num
print ' over '
---------------------------------
#coding =utf8
#遇到2重新开始下一次循环
num = 0
while (Num < 8):
num = num + 1
if num = = 2:
Continue
Print num
print ' over '
---------------------------------
For loop #Python for loop Unlike other languages, Python's for loop is primarily used to traverse sequences and strings.
Format 1:
For variable in sequence/string:
EXECUTE statement
---------------------------------
List = [1,3,5, ' TT ', ' 999 ']
#每次从序列 [1,3,5, ' TT ', ' 999 '] to take out an element and assign the element to X
For x in list:
Print X
---------------------------------
#内置函数range () #新建一个序列. The elements of this sequence are integers, and 0 begins with the next element 1 greater than the previous one, until the upper limit written in the function-1
For x in range (10):
Print x #输出0
---------------------------------
IDX = Range (10)
If as a judgment, must have to judge with the object to be judged, so must first declare a, otherwise error
If a in IDX:
Print A * * 20
For is to assign the value of each loop traversal to a, and then use a
For a in IDX:
Print A * * 20
Python Basics 7-Cycle of Process Control