This article explains how to use flow control statements in python and explains them through instances.
1. while statement
Conditional loop control statement. It usually needs to be used with break, otherwise it will enter an endless loop.
Format: [while <条件> :
<内容>
Break]
X = int (input ('Enter a number: ') while x> 0: print ('positive number') break
2. if statement
Conditional control of process branches, generally used with elif and else.
X = int (input ('Enter a number: ') if x <0: print ('negative number') elif x = 0: print ('0') else: print ('positive number ')
A simple if else statement can be represented by a triplicate operation.
# Writing format result = value1 if condition else value2 # if the condition is true, the value of value1 is assigned to result. if the condition is not true, the value of value2 is assigned to resul.
3. for statement
A loop control statement can be used to traverse an object and use it with in.
Format: [for <> in <对象集合> :]
X = ['A', 'B', 'C', 'D'] for I in x: # I position characters, as long as they are not keywords, print (I)
4. range () function
Number sequence iterator, when you iterate on it, it is an object that can return continuous items as expected sequence, but to save space, it does not really construct the list.
Format: range (stop) indicates the end value. The default start value is 0 and the interval is 1.
Range (start, stop) is the start value and the end value. The interval is 1.
Range (start, stop, step) indicates the start value and the end value. The interval is the step value.
For I in range (3): # The running result is, 2 print (I) for I in range (): # The running result is, 4 print (I) for I in range (-, 2): # The running result is-, print (I)
5. break and continue statements, as well as the else statements in the loop
1) the break statement is similar to the one in C and is used to jump out of the last level for or while loop.
while True: print('hello') break
2) continueThe statement indicates that the loop continues to execute the next iteration:
For x in range (1, 4): print (x, 'For clause ') continue print (x, 'After the continue clause') else: print (x, 'else') # running result for statement else statement
3) the else in the loop
For example, in the continue example, if a for-else statement exists, the else statement is executed after the loop jumps out, but the break jumps out of the loop and does not execute else, therefore, else can be used to handle exceptions in the loop.
For x in range (1, 4): print (x) else: print (x) # run result 23
6. pass statement
The pass statement does not do anything. It is used for statements that require syntax, but the program does nothing. it is usually used to create a class with the minimum structure.
On the other hand, pass can be used as a placeholder for a function or control body when a new code is created. It allows you to think at a more abstract level.
class EmptyClass: pass
The above is the details about the Python process control code sharing. For more information, see other related articles in the first PHP community!