How to use else statements in loops in Python
It is believed that many inexperienced people have no idea about how to use the else statement in the loop in Python. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
1. Else statement module in the loop
1. The else statement inside the loop, if used with the If statement, is the normal flow of the if statement.
2. The else statement outside the loop is directly used with the loop statement while or for, then it means that if the loop statement has not been jumped out of the break, the else statement connected to the outside of the loop will be executed.
II. Supplement: the use of semicolons
It is to separate the function of our variables, to distinguish what statements are used, to use them only when multiple statements appear in one line. By default in Python, a line of code is thought of as a statement.
Third, supplement range function to pass parameters.
If a value is passed in by default, then the default starting position is 0, which is still a half-open and semi-closed interval, and the last value is not included.
Note: the else statement module used with the loop statement is mutually exclusive with the break in the loop statement.
IV. Summary and emphasis
1. Understand the supplement of range function
2. Understand the supplement of semicolons
3. Master the combination of else statement and while sentence
4. Master the combination of else statement and for sentence
This section of the knowledge source code 1 while else is used with the source code:
Xerox 9: 10
While x