Python study notes - loop

while loop

The general form in Python while statement:

while 判断条件(condition):
    执行语句(statements)……

Note the colon and indentation . In addition, no do ... while loop in Python.

The following example uses the sum is calculated while 1 to 100:

n, sum = 100, 0
counter = 1

while counter <= n:
    sum = sum + counter
    counter += 1

print("1 到 %d 之和为: %d" % (n, sum))

Execution results are as follows:

And it is 1 to 100: 5050

Infinite loop

We can never achieve infinite loop to false by the conditional expression, examples are as follows:

var = 1
while var == 1 :  # 表达式永远为 true
   num = int(input("输入一个数字  :"))
   print ("你输入的数字是: ", num)
 
print ("Good bye!")

The implementation of the above script, output results are as follows:

Enter a number: 5
The number you entered: 5
Enter a number:

You can use CTRL + C to exit the current infinite loop.
Infinite loop in real time to client requests very useful on the server.

while recycling else statement

In the execution else while ... else conditional statement is false in order to block.
Syntax is as follows:

while <expr>:
    <statement(s)>
else:
    <additional_statement(s)>

for statement

Python for loop can iterate any sequence of items, such as a list or a string.
The general format for the following cycle:

for <variable> in <sequence>:
    <statements>
else:
    <statements>

The following examples are used for the break statement, break out of this loop statement is used:

sites = ["Baidu", "Google","Runoob","Taobao"]
for site in sites:
    if site == "Runoob":
        print("菜鸟教程!")
        break
    print("循环数据 " + site)
else:
    print("没有循环数据!")
print("完成循环!")

After executing the script, when the cycle to "Runoob" will jump out of the loop:

Cyclic data Baidu
Google circulation data
Rookie tutorial!
Completing the cycle!

range () function

If you need to traverse a sequence of numbers, you can use the built-in range () function. It will generate the number of columns, for example:

for i in range(5):
	print(i)

0 1 2 3 4

#指定区间
for i in range(5,9) :
    print(i)

5 6 7 8

#步长(可以为负数)
for i in range(0, 10, 3) :
    print(i)
    
0 3 6 9

for i in range(-10, -100, -30) :
    print(i)
  
-10 -40 -70

break and continue statements and loop else clause

Here Insert Picture Description

break statement can jump out of the loop for a while and. If you break out or for while loop, any corresponding loop else block is not executed.
continue statement is used to tell Python skip the remaining statements in the current loop block, and then proceed with the next cycle.

Loop else clause can have it in an exhaustive list (with a for loop) or the condition becomes false (with while loop) cause to be executed when the loop terminates, but does not perform the loop is terminated break.
Examples The following examples for circulating the mass number of queries:


```python
for n in range(2, 10):
    for x in range(2, n):
        if n % x == 0:
            print(n, '等于', x, '*', n//x)
            break
    else:
        # 循环中没有找到元素
        print(n, ' 是质数')

Implementation of the above script output is:

2 Is a prime number
3 Is a prime number
4 Equal to 2 * 2
5 Is a prime number
6 Equal to 2 * 3
7 Is a prime number
8 Equal to 2 * 4
9 Equal to 3 * 3

The pass statement

Python pass is an empty statement, in order to maintain the integrity of the program structure.
pass without doing anything, generally used as a placeholder statement.

Python3 loop

Published 11 original articles · won praise 3 · Views 931

Guess you like

Origin blog.csdn.net/qq_36551226/article/details/104138096