Home > Backend Development > Python Tutorial > How to limit the number of loops in Python

How to limit the number of loops in Python

爱喝马黛茶的安东尼
Release: 2019-06-17 16:42:09
Original
19960 people have browsed it

How to limit the number of loops in python?

Related recommendations: "python Video"

How to limit the number of loops in Python

The while statement in Python programming is used to execute the program in a loop, that is, under certain conditions , execute a certain program in a loop to handle the same tasks that need to be processed repeatedly. Its basic form is:

while Judgment condition:
Execution statement...

The execution statement can be a single statement or a statement block. The judgment condition can be any expression, and any non-zero or non-null value is true.

When the judgment condition is false, the loop ends.

#!/usr/bin/python
count = 0
while (count < 9):   
    print &#39;The count is:&#39;, count
   count = count + 1
print "Good bye!"
Copy after login

The above code execution output results:

The count is: 0
The count is: 1
The count is: 2
The count is: 3
The count is: 4
The count is: 5
The count is: 6
The count is: 7
The count is: 8
Good bye!
Copy after login

There are two other important commands when the while statement is continue, break is used to skip the loop, continue is used to skip the loop, break is It is used to exit the loop. In addition, the "judgment condition" can also be a constant value, indicating that the loop must be established. The specific usage is as follows:

# continue 和 break 用法
 i = 1while i < 10:   
    i += 1
    if i%2 > 0:     # 非双数时跳过输出
        continue
    print i         # 输出双数2、4、6、8、10
 i = 1while 1:            # 循环条件为1必定成立
    print i         # 输出1~10
    i += 1
    if i > 10:     # 当i大于10时跳出循环
        break
Copy after login

The above is the detailed content of How to limit the number of loops in Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template