Conditional statements are common in programming languages. Whenever you need to write a program, you will almost certainly use if… else … Such conditional statements make conditional judgments. So all common programming languages provide if… else … Syntax like this. Python is no exception, but Python is more than just if… else … Such syntax, it also provides for… else … , try… except … else … And so on. Today we are only going to discuss for… else … Syntax, let’s see how Python’s else statement follows for versus if.

Let’s start with a code example:

for i in [1.2.3] :print(i)
else:
    print('done')
Copy the code

Execution Result:

1
2
3
done
Copy the code

If you didn’t execute this code and just looked at the way it was written, you might guess that the result would be 1, 2, 3, and no done. Because in if… else … If the condition is true, then the code block under the else statement will not be executed. But according to the printed results, it obviously does not meet our expectations. With that in mind, let’s look at the following example code:

for i in[] :print(i)
else:
    print('done')
Copy the code

Execution Result:

done
Copy the code

This time the result is only one done, so this code looks more reasonable. Because the list for traverses is empty, the condition is not true, so else logic should be executed, is that true? Let’s move on to the following example. We know that the for block can contain both continue and break keywords, so let’s test what happens if the for block contains continue.

for i in [1.2.3] :if i == 2:
        continue
    print(i)
else:
    print('done')
Copy the code

Execution Result:

1
3
done
Copy the code

This result is similar to the first code example, except that at I == 2, the loop is skipped and the next loop continues. Finally, let’s look at what happens if the for block contains a break.

for i in [1.2.3] :if i == 2:
        break
    print(i)
else:
    print('done')
Copy the code

Execution Result:

1
Copy the code

Based on the print of the code above, I think you’ve probably figured out the Python for… else … Execution characteristics of a statement. Yes, in fact, if you follow if… else … Is mutually exclusive logic to understand for… else … The break inside the for block is really a pair of conditional statements with the else. If a break statement is executed in the for loop, the code inside the else block is no longer executed.

Knowing Python’s for… else … Statement syntax, but what does it do? I first got involved with for… else … It was so useless that I wrote Python code for so long that I didn’t use it, and I almost forgot that Python provided it. It wasn’t until I started writing ERP projects that I realized for… else … The real use of. Because writing ERP system will encounter a variety of complex business requirements, if can use for… else … Solve the problem and the code logic will look much clearer, greatly increasing the maintainability of the project.

If the for loop iterates through an iterable, a logic is executed if one of the iterable elements meets the criteria, and then a break is broken. If none of the iterable elements meets the criteria, an else statement is executed to execute another logic.

for i inIterable:ifConditional judgment: Perform a logicbreak
else: Executes another logicCopy the code

This code obviously doesn’t use for… else … Is more readable and more Pythonic.

flag = False
for i inIterable:ifCondition check: Execute a logical flag =True
        break

if notFlag: Executes another logicCopy the code

Above all, this is my opinion on… else … Hope to be helpful to you when you encounter the same problem.