This article mainly introduces the basic conditions and cycle control of Python3. If you need it, you can refer to the example in this article to explain the conditions and cycle control statements of Python3 and their usage, it is an important knowledge point to learn Python. It is shared with you for your reference. The details are as follows:
In general, Python's flow control statements include: if condition statements, while loop statements, for loop statements, range functions, and break, continue, and pass control statements. The semantics of these statements in Python is basically the same as that in other languages, so here we only talk about their usage.
I. if statement
The if statement is the most commonly used condition control statement. The general format in Python is:
If condition 1: statementselif condition 2: statementselse: statements
In Python, elif replaces else if, so the keyword of the if statement is: if-elif-else.
Note:
1. Each condition is followed by a colon (:), indicating the statement block to be executed after the condition is met.
2. Use indentation to divide statement blocks. statements with the same number of indentation form a statement block.
3. There is no switch-case statement in Python.
The sample code is as follows:
x = int(input("Please enter an integer: "))if x < 0: print('Negative.')elif x == 0: print('Zero.')else: print('Positive.')
Ii. while statement
The general form of the while statement in Python:
While judgment condition: statements
Pay attention to the colon and indentation. In addition, there is no do... while loop in Python.
The sample code is as follows:
A, B = 0, 1 while B <10: # cyclic output Fibonacci series print (B) a, B = B, a + B
III. for statements
The for statement in Python is a little different from the for statement in C language: The for statement in C language allows users to customize iteration steps and termination conditions, while the Python for statement can traverse any sequence (sequence ), the elements are iterated sequentially in the sequence of appearance. The general format is:
for variable in sequence: statementselse: statements
The sample code is as follows:
words = ['cat','love','apple','python','friends']for item in words: print(item, len(item))
If you need to modify the sequence of your positive iteration in a loop, you 'd better make a copy. In this case, the slice mark is very useful:
Words = ['cat', 'love', 'apple', 'python', 'ds Ds'] for item in words [:]: # create a sliced copy of the entire list if len (item)> = 6: words. insert (0, item) print (words)
We have noticed that the else clause can also be used in loop statements. The fifth point is described below.
Iv. range Function
If you want to traverse a sequence of numbers, the built-in range () function can be used. The range () function is often used in a for loop to generate an arithmetic sequence:
>>> List (range (10) # The default value ranges from 0 to [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> list (range (1, 11) # from 1 to 11, open [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> list (range (0, 30, 5) #5 indicates the step size. Get a number every five [0, 5, 10, 15, 20, 25]
The sample code is as follows:
for i in range(2, 11): print(i)
5. break, continue, pass, and else clauses
①. Break
Similar to the C language, the break statement jumps out of the recent for or while loop.
②. Continue
The continue statement is also borrowed from the C language. It terminates the current iteration and carries out the next iteration of the loop.
③. Pass
The pass statement does not do anything. It only requires one statement in syntax, but the program does not need any operation. The pass statement is used to maintain the integrity of the program structure.
④ Else clause
You can also use the else clause in the loop statement. The else clause is executed when the for statement or the loop condition is false (while statement, however, the loop is terminated by the break and is not executed. As follows:
# Run the else clause for I in range (2, 11): print (I) else: print ('for statement is over. ') # The else clause for I in range (5): if (I = 4): break; else: print (I) else: print ('for statement is over') # No output