In the study of python loop statement, found that else can be used with the loop statement, but it is completely different from the run of the else statement in the if, sometimes you really feel like falling into this else trap, do not know how to use, so now let's go together to see it!
I. Loop_else example
1、while_elseutilization
# Define the outer loop counter i = 1 # Cycle through each number from 1-10 while i <= 10: # Define the inner loop counter j = 1 # Cycle through each number 1-5, or 5 times while j <= 5: # Determine if a number between 1 and 10 / 2 has a remainder if i % 2 == 0: # If there is no remainder then we jump out of the inner loop and continue with the outer loop. break # Modify the inner loop counter j += 1 # Execute else when the loop ends normally else: print("Less than") i += 1
Output results:
less than
less than
less than
less than
less than
(1) Analyze: why do you output 5 less than?
1, first of all, we should know that break is the closest to which loop, then it will work in which loop, the above is in the inner loop, then it will work in the inner loop. Secondly, the inner loop is in the outer loop, so the inner loop is a statement for the outer loop. python language pays special attention to the code format, the outer loop will see the inner loop as a statement, then the problem is very simple.
2, the second else and that loop alignment, then the else belongs to the loop, when the loop will be executed after the normal end of the else statement, otherwise encounter break will jump out of the loop, then will not be able to execute the else statement
3, look at the code, the outer loop to cycle 10 times, the inner loop to cycle 5 times, that is to say, the outer loop 1 time, the inner loop to cycle 5 times. There is a condition in the inner loop, when i % 2 == 0, then jump out of the loop, 1-10 can be divided by 2 is an even number, then when i = 2, 4, 6, 8, 10 of these 5 cases, it will jump out of the inner loop, at this moment, else is not implemented, then the implementation of the 5 times else is i = odd, the condition does not hold true, so it will be the normal end of the inner loop, the final implementation of the Finally, the statement in else will be executed. So it means that the else statement will be executed only when the while loop ends normally, if it is broken, it will not be executed.
(2) Analyze: why does changing the inner loop counter to j = 6 output 10 less than?
Since the inner loop condition does not hold, there is no access to the loop body, and no looping, so the statement in else will be executed unconditionally, which then means that when the condition does not hold, the statement in else will be executed unconditionally
2. The use of if_else
# Cycle 10 times and get a number between 1 and 10 # for i in range(1,11): # Circulate five times and get a number between 1 and 5 # for j in range(1,6): # Determine if the value of i is divisible by 2 if i % 2 == 0: # If yes, jump out of the inner loop break # Execute the else statement when the loop ends normally. else: print("Greater than")
Output results:
more than
more than
more than
more than
more than
(1) Analyze: why do you output 5 greater than?
First of all, it is important to know that using else is the same thing whether it is a while loop or a for loop; else will be executed only when the loop ends normally, otherwise the else statement will not be executed.
The outer loop loops 10 times, the inner loop 5 times, the outer loop 1 time, the inner loop 5 times. When the condition of the inner loop is established, break will be executed, and the outer loop will be executed, and when the condition is not established, it is equivalent to not having this condition, and the else statement will be executed directly. In other words, when the loop meets break, it will jump out of the loop without executing else, and if it doesn't meet break, it indicates that the loop ends normally, and it will go to execute the else statement.
for i in range(1,11): for j in range(1,1): if i % 2 == 0: break else: print("Greater than")
(2) Analyze: why would you output 10 greater than?
By the same token, since range(1,1) indicates that there is no loop count, the for loop fails to execute, so it goes to the else statement. That is, when the loop fails to execute, the else statement is executed.
3、What are the scenarios for while loop and for loop?
while loop | When the number of loops is unknown, a while loop is appropriate. Of course, it can also be used when the number of loops is known, but this can sometimes increase the amount of code. |
---|---|
for loop | Use for loops when the number of loops is known, and for loops are beautiful when iterating over lists, tuples, strings, and dictionaries, and can be customized to iterate over elements. |
II. Summary
Whether it's a while loop or a for loop, it's the same thing when used together with else. When the loop ends normally, it will execute the else statement; if it breaks and ends early, it will not execute else; when the loop fails to execute, it will automatically execute the else statement.
to this article on the use of python else statement in the loop detailed article is introduced to this, more related to the use of python else loop content please search my previous posts or continue to browse the following related articles I hope that you will support me in the future more!