Home > Article > Backend Development > Unique loop statements and their characteristics in Python programming
The pursuit of Python programming is simplicity and elegance, which is omnipotent and close to people’s natural expression mode. Undoubtedly, its simple but not simple magic attracts a large number of fans. Even if its syntax and built-in functions can be abbreviated or abbreviated, it will never be written out completely. If you are used to this way of writing, and then look at the "Spring-style" long names, you will almost wonder why you typed the keyboard so many times in the first place - is it for exercise?
I went a bit far, back to the topic. We know that there are only several types of control statements for the standard code execution flow: sequential execution, conditional execution, loop execution, and combinations or/and nesting of these. Various programming languages (such as C/C, Java, JavaScript, PHP, go, etc.) have different implementations of specific syntax, but they are basically the same: if-else structure, while structure, for structure, etc.
What I want to talk about here is the conventional expansion mode of Python's loop structure grammar. Taking while as an example, the conventional grammar structure is as follows:
while condition: #循环体 pass
This is a typical usage form. Its expanded structure is as follows:
while conditon: #循环体 pass else: #while循环体执行完 pass
The difference between this while-else structure and the if-else structure is that only one of the situations (that meets the conditions) can be executed under the if structure, while the while structure usually has two parts. will be implemented. An example is as follows:
counter =0 while counter<5: counter+=1 print(f"counter={counter}") else: print(f"while结束了,进入else部分:当前counter={counter}")
Run the above code, the output is as follows:
counter=1
counter=2
counter=3
counter=4
counter=5
while is over, enter the else part: current counter=5
This code has no story and is easy to understand. But let me change the form and see the effect:
counter =0 while True: counter+=1 print(f"counter={counter}") if counter>=5: break else: print(f"while结束,else部分...,counter={counter}")
Run the program, the output is similar to the following:
counter=1
counter=2
counter =3
counter=4
counter=5
It’s over, the else part will not be executed...
Of course, Python’s for-else The structure is the same, so I won’t give an example here. If you are interested, try it yourself. What I want to say here is that in Python's "loop-else" structure, if you use break to propose a loop, it will jump out of the entire loop structure, and the else part of the loop is also part of the entire loop. Therefore, if you want to do some finishing work after the loop body is finished, you must be careful not to use break to jump out.
When I introduced this structure to students, I said that this grammar seemed to be useless. Doesn’t your Python pursue simplicity? Add an else to while/for. Wouldn’t it die without it? What you want to do in else can be done without else.
Later, I followed Python's "humanized" guiding philosophy and thought about it again: It's like eating in a restaurant. You finish the meal and pay as usual (normal thing). When you go out, you grab a handful of sugar at the door of the hotel and pour it into a cup. Water (cleaning up work); but if you eat a foreign object while eating and can no longer continue to enjoy the meal (abnormal termination - break), how can you still be in the mood to drink water and eat sweets? ——Maybe that’s what it means ^_&.
Python programming is still very interesting, but it is more about using Python to solve practical problems. Therefore, Python syntax is easy to learn, but there are many libraries and frameworks that need to be learned to be able to use Python flexibly to solve problems.
That’s it for today. I’ll write about other Python-specific programming implementations next time.
The above is the detailed content of Unique loop statements and their characteristics in Python programming. For more information, please follow other related articles on the PHP Chinese website!