[Notes & Tutorial] Python Basics 6- Loop Structure

Table of contents

The usefulness of the loop structure

for loop

structure

Iterate over iterable objects

Loop the specified number of times

range function

Comment 

while loop

structure

Example

infinite loop

break statement 

continue statement 

loop nesting

for

while


 

 

The usefulness of the loop structure

There are many uses of loops, and they are also very commonly used. The following is my summary:

  1. Specify the number of times to repeatedly execute a certain code or function
  2. Repeatedly execute a certain piece of code or function when conditions are met
  3. Traverse iterable objects (iterators)

"Traversal" is to get the value of the iterated object one by one. Next, learn about loops in the Python language.

for loop

The for here does not mean "for" in English. It is a loop statement (a Python keyword) that can traverse a sequence and repeat it a specified number of times.

structure

for i in seq:
 代码块

When functioning, specify the variable i to traverse the iterative object, that is to say, take out the contents of an iterative object one by one and assign them to i

Iterate over iterable objects

>>> seq=[1,2,3,4,5]
>>> for i in seq:
...     print(i)
...
1
2
3
4
5

The function of the above code is to traverse the list of seq (will be discussed later), and then assign the values ​​​​in it to i one by one, and output i each time

Loop the specified number of times

The basic idea of ​​looping a specified number of times is to create an iteration object with a length of a specified number of times. The number of times the for statement traverses is the specified number of times. Isn't it wonderful... So, in essence, it is still traversing the iteration object.

for i in range(循环次数):
    代码块

The range function is used above, which is a built-in function in python that can create a specified sequence.

 So the specified number of loops can be written like this:

>>> for i in range(10):
...     print("循环")
...
循环
循环
循环
循环
循环
循环
循环
循环
循环
循环

range function

range(起始值(可选),终止值(必填),间隔(可选))

 This function can create a sequence of integers, for example 

>>>a=range(10)
>>>print(list(a))#list函数是将其他类型的变量或值转化为list列表类型的函数,这样方便查看我们创造的range对象里的值
>>>#若直接输出a会得到:range(0,10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> print(list(a))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> b=range(1,10,2)
>>> print(list(b))
[1, 3, 5, 7, 9]

Comment 

The words after "#" in the above code are comments in Python and will be ignored at runtime. Multi-line comments also use the characters ''' ''', such as:

'''注释内容'''
#注释内容

while loop

structure

while 条件表达式:
    代码块

 Execute a block of code until a condition is met

Example

>>> a=0
>>> while a<10:
...     print(a)
...     a+=1
...
0
1
2
3
4
5
6
7
8
9

The function of the above code is that when a is less than 10, it will always output a and let a increase by 1 (for the += auto-increment operator, please see this column: Python Basic Tutorial 4- Operator )

The above method is also a method of using while loop to count loops. Don't forget to let the loop variable write itself in the code block, otherwise it will become an endless loop - an infinite loop.

infinite loop

But sometimes we may need an infinite loop (for example... it's pygame...). It's very simple. Smart readers must have thought of a way to directly make the condition true without using variables.

while True:
    代码块

⚠️If the while loop is used improperly, it will become an infinite loop. Sometimes an infinite loop is not good. It will occupy resources, cause lags, and program crashes... 

break statement 

The break statement in Python means to jump out of the current loop. It is used to jump out when conditions are met. It is usually paired with an if statement. For example:

>>> for i in range(10):
...     if i==6:
...         break
...     print(i)
...
0
1
2
3
4
5

Jump out when the condition that i is 6 is met

continue statement 

The function of the continue statement is to skip this cycle and enter the next cycle. For example:

>>> for i in range(10):
...     print(i,end=" ")
...     if i==6:
...             continue
...     print(i+1)
...
0 1
1 2
2 3
3 4
4 5
5 6
6 7 8
8 9
9 10

It can be seen that the following print(i+1) is skipped when the cycle reaches the 7th time, and the next cycle is directly entered.

loop nesting

Loops can be nested, both for and while can have multiple loops

for

for i in 迭代对象1:
    for j in 迭代对象2:
        代码块
    代码块

while

i=0
while i<循环次数1:
    j=0
    while j<循环次数2:
        代码块
    代码块

Of course, i and j here can be replaced, but they are usually used as the names of loop variables, and they can be nested all the time

 

Guess you like

Origin blog.csdn.net/m0_61316509/article/details/128535483