I. Cyclic structure
In Python, there are two operations for the For loop and while loop, with no do......while statements.
1.for Loop:
Unlike the common notation for loops in other languages such as for (int i=0;i<10;i++), the for loop in Python is more similar to the foreach structure:
# #举例: Traverse all letters that output a single word
For letter in'Python':
Print(' current letter:'+ letters ')
Contrast:
string " Python " ; foreach (charin str) { Console.WriteLine (" current Letter:"+C) ; }
We sometimes involve operations or judgments on an array, a list, or a dictionary index or subscript, and in Python we provide a for-I in range (start index, end index) operation:
# #举例: Outputs the first 3 letters of a list list = [ " c# " , Span style= "COLOR: #800000" > " java " , " python ", " c ", " c++ " " for I in Range (0,2) # #注意索引下表是从0开始, this notation is equivalent to the for (int i=0;i<=2;i++) print (List[i])
In Python3, we can even make range start with a specified number and specify different increments (even negative numbers, sometimes referred to as ' stride '):
For example: For i in range (0, 3) : print(i ), this notation is equivalent to the for (int i=0;i<=10;i=i+3)
< Span class= "Hl-number" >2.while loop:
The basic syntax is as follows:
while Judging Condition: Loop statement
When judging the condition as a perpetual condition, while is an infinite loop, it is more common to handle real-time requests or other looping operations:
while 1==1: ##循环语句
Use Crtl+c to jump directly out of the loop
3. Use break with continue for loop control:
var = while var > 0: print ( ' " , var) var = Var-1 if var = = 5: break
# #break直接跳出整个循环 print (" good bye! ")
var = ten while var > 0: = var-1 if var = = 5: # continue to skip this Loop's remaining code, jump directly into the next loop continue print (' current variable value:') , var)print ("good bye! ")
Python Learning Notes (Ⅱ)--loops/selections/functions