Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Why not write else blocks after for and while loops

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "Why not write the else block after the for and while cycle". The explanation in the article is simple and clear and easy to learn and understand. Please follow the editor's train of thought to study and learn "Why not write the else block after the for and while cycle"!

In practical work, the author will not write in this way, but will use auxiliary functions to complete the calculation. There are two common ways to write such auxiliary functions.

The first way to write is to return as soon as a condition is found to be true, and if this situation is never encountered, the loop executes completely, allowing the program to return the value at the end of the function as the default return value.

Def coprime (a, b): for i in range (2, min (a, b) + 1): if a% I = = 0 and b% I = 0: return False return True assert coprime (4,9) assert not coprime (3,6)

The second way to write is to use variables to record whether there is such a situation during the loop, if so, use break to jump out of the loop in advance, if not, the loop will be fully executed, in any case, the value of the variable will be returned.

Def coprime_alternate (a, b): is_coprime = True for i in range (2, min (a, b) + 1): if a% I = 0 and b% I = 0: is_coprime = False break return is_coprime assert coprime_alternate (4,9) assert not coprime_alternate (3,6)

For those who are not familiar with the structure of for/else, the two writing methods just now are relatively clear, and you can choose one of them according to the situation.

Although the for/else or while/else structure itself can achieve some logical expressions, the confusion it brings to the reader (including yourself) has outweighed its benefits. Because the simple structure of for and while loops should be quite clear to read in Python, if you put the else block right after it, it will make the code ambiguous. So, please don't write that.

Keystone

Python has a special syntax for placing else blocks immediately after the entire for loop or while loop.

The else block executes only if the entire loop does not jump out early because of break.

Putting the else block immediately after the entire loop makes it difficult to see what this code means, so avoid writing it this way.

Python loops have a feature that is not supported by most programming languages, that is, else blocks can be placed immediately after the entire loop structure.

For i in range (3): print ('Loop', I) else: print > Loop 0 Loop 1 Loop 2 Else block!

Oddly enough, the program executes the contents of the else block after completing the entire for loop. If that's the case, why is it called "else"? This should be called "and". In the if/else structure, else means that if the previous statement is not executed, the else block is executed. In the try/except structure, except also means the same: if the previous statement fails, execute the except block.

The else in the try/except/else structure still follows the idea that if there are no exceptions to handle, execute this statement. The finally in the try/finally structure is also intuitive, meaning that no matter how the previous piece of code executes, the finally block code is executed at the end.

Knowing the use of else, except, finally, and so on in the above structures, the novice to Python may think that the else in the for/else structure also means that if the loop is not executed from beginning to end, then execute the else block. In fact, on the contrary, if the loop is not executed from beginning to end (that is, the loop terminates prematurely), the code in the else block will not be executed. Using the break statement in a loop actually skips the else block.

For i in range (3): print ('Loop', I) if iTunes 1: break else: print (' Else b1ockstores') > Loop 0 Loop 1

Another strange thing is that if you do a for loop on a blank sequence, the program will immediately execute the else block.

For x in []: print ('Never runs') else: print > For Else block!

The same is true of the while loop, where if the first loop encounters False, the program runs the else block immediately.

While False: print ('Never runs') else: print > While Else block!

Else is designed like this so that you can use it to implement search logic.

For example, if you want to determine whether two numbers are prime or not (that is, no number other than 1 can divide them at the same time), you can use this structure. First try the number of numbers that are possible to divide them at the same time, and if you still don't find such a number after all, the loop will be executed from beginning to end (which means the loop didn't jump out early because of break), and then the program will execute the code in the else block.

A = 4 b = 9 for i in range (2, min (a, b) + 1): print ('Testing',i) if a% I = = 0 and b% I = 0: print (' Not coprime') break else: print ('Coprime') > Testing 2 Testing 3 Testing 4 Coprime

In practical work, the author will not write in this way, but will use auxiliary functions to complete the calculation. There are two common ways to write such auxiliary functions.

The first way to write is to return as soon as a condition is found to be true, and if this situation is never encountered, the loop executes completely, allowing the program to return the value at the end of the function as the default return value.

Def coprime (a, b): for i in range (2, min (a, b) + 1): if a% I = = 0 and b% I = 0: return False return True assert coprime (4,9) assert not coprime (3,6)

The second way to write is to use variables to record whether there is such a situation during the loop, if so, use break to jump out of the loop in advance, if not, the loop will be fully executed, in any case, the value of the variable will be returned.

Def coprime_alternate (a, b): is_coprime = True for i in range (2, min (a, b) + 1): if a% I = 0 and b% I = 0: is_coprime = False break return is_coprime assert coprime_alternate (4,9) assert not coprime_alternate (3,6)

For those who are not familiar with the structure of for/else, the two writing methods just now are relatively clear, and you can choose one of them according to the situation.

Although the for/else or while/else structure itself can achieve some logical expressions, the confusion it brings to the reader (including yourself) has outweighed its benefits. Because the simple structure of for and while loops should be quite clear to read in Python, if you put the else block right after it, it will make the code ambiguous. So, please don't write that.

Keystone

Python has a special syntax for placing else blocks immediately after the entire for loop or while loop.

The else block executes only if the entire loop does not jump out early because of break.

Putting the else block immediately after the entire loop makes it difficult to see what this code means, so avoid writing it this way.

Thank you for your reading, the above is the content of "Why not write else blocks after for and while cycles". After the study of this article, I believe you have a deeper understanding of why you should not write else blocks after for and while cycles. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report