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

How to realize while Infinite iterative Loop in Python

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to realize the while infinite iterative loop in Python". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to realize the infinite iterative loop of while in Python".

Preface

Python has while statements and for statements handled as loops. Although the for statement has a certain number of processes, the while statement is a circular process of type "until the condition is met".

For infinite iteration while, the number of times the loop is executed is not explicitly specified in advance. On the contrary, the specified blocks are repeated as long as certain conditions are met.

Iterating through the for with a definition, the number of times the specified block will be executed is clearly specified at the beginning of the loop.

In addition to the general features of while statements, Python also has its own specifications, such as insufficient support for do while statements. Loop processing is the basic syntax of programming.

While cycle

While:

# Loop body

A control expression that usually involves one or more variables that are initialized before the loop starts and may then be modified somewhere in the loop body.

When while encounters a loop, it is first evaluated in Boolean context.

N = 5while n > 0: n-= 1 print (n)

Output:

four

three

two

one

0

While first tests the control expression of the loop. Assuming that it is false at the beginning, the loop body will never be executed.

N = 5while n > 5: n-= 1 print (n) break statement and continue statement

The entire body of the while loop is executed in each iteration, and Python provides two keywords to prematurely terminate the iteration of the loop.

The break statement immediately completely terminates the loop. The program executes the first statement that continues after the loop body.

The continue statement immediately terminates the current loop iteration. Execute to jump to the top of the loop and re-evaluate the control expression to determine whether the loop is executed again or terminated.

# break example n = 5while n > 0: n-= 1 if n = = 2: break print (n) print ('loop ends.')

Output:

four

three

The cycle is over.

# continue example n = 5while n > 0: n-= 1 if n = = 2: continue print (n) print ('loop ends.')

Output:

four

three

one

0

The cycle is over.

Else clause

Python allows the use of optional clauses at the end of a loop else.

While: # Loop body else: n = 5while n > 0: n-= 1 print (n) else: print ('Loop done.')

Output:

four

three

two

one

0

Loop done.

# if there is break, n = 5while n > 0: n-= 1 print (n) if n = 2: breakelse: print ('end of loop.')

Output:

four

three

two

Infinite cycle

Suppose you write a loop that while theoretically never ends.

While True: print ('true three countries are free') true three countries and true three countries are free. . . Traceback (most recent call last): File "", line 1, in KeyboardInterrupt

Such a loop can only be stopped manually.

A single actually has its applications, such as cycling to delete elements from a list.

List_ = ['True three Kingdoms free', 'True three Kingdoms free', 'True three Kingdoms free'] while True: if not list_: break print (list_. Pop (- 1))

Output:

The true three Kingdoms are free.

The true three Kingdoms are free.

The true three Kingdoms are free.

You can break to specify multiple statements in a loop. You can end the loop from several different locations through break without having to specify all the termination conditions in the loop header.

While True:

If: # conditional judgment 1

Break

If: # conditional judgment 2

Break

If: # conditional judgment 3

Break

Nested while loop

Python control structures can be nested within each other.

If age

< 18: if gender == 'M': print('子供') else: print('娘')elif age >

= 18 and age

< 65: if gender == 'M': print('父親') else: print('母親')else: if gender == 'M': print('おじいさん') else: print('祖母') while循环可以包含在另一个while循环中。 list_ = ['父親', '母親']while len(list_ ): print(list_.pop(0)) list__ = ['おじいさん', '祖母'] while len(list__ ): print('>

', list__.pop (0))

Output:

Father's wife

> please tell me what to do

Grandma

Mother

> please tell me what to do

Grandma

The break statement found in the nested loop applies to the nearest closed loop.

While: statement statement while: statement statement break # for while: loop break # for while: loop

While loops can be nested in if, elif, else statements.

If:

While:

Else:

While:

While:

If:

Elif:

Else:

If:

One-line while loop

Like the if statement, while can specify a loop on one line. It can also be used to form multiple loop body statements.

N = 5while n > 0: n-= 1; print (n)

Output:

four

three

two

one

0

It is not possible to combine two compound statements into shorthand.

If True: print ('data') datawhile n > 0: n-= 1; if True: print (' data') SyntaxError: invalid syntax thank you for reading, the above is the content of "how to realize the infinite iterative loop of while in Python". After the study of this article, I believe you have a deeper understanding of how to realize the infinite iterative loop of while in Python, and the specific use needs to be verified in practice. 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