worth collecting! 16 of the code entry Python loop

[REVIEW] This article focuses for statements and while statements. for statement belongs to traverse the cycle, while circulating the statement as belonging. In addition to the two loop, also describes the break, continue with a three pass sentence for controlling the cyclic structure of the program flow. On this basis, also describes the list comprehension, which is a special kind of loop.

Repeating loop structure is also known, is repeatedly used to perform an operation. The face of repeated operation of large magnitude, even if the use of computer, write code that is repeated time-consuming, requiring the aid of loop. Use loop to general use condition judgment as to determine whether the return value of the formula loop body is executed in accordance with.

Cycle is divided into two modes, one is the loop body is executed when the condition is satisfied, another contrast, loop body is executed when the condition is not satisfied. The former is called when circulating, circulating until the latter is referred to.

In FIG 1, when the dotted box is a loop-type structure that contains the loop and the determination condition, and the wire connection portions of the flow. When the program is executed, first determine the true and false condition. Determination is true, the loop is executed; determination is false, the loop is not executed, the loop ends. When the first circulating conditional judgment, if the loop condition is satisfied, then loop body is executed, it is also known circulating prior to testing.

Here Insert Picture Description
In FIG. 2, within the dashed box until a circulating structure, which comprises determining conditions and the body of the loop, and a flow line connecting the portions. When the program is executed, to perform a loop, then determining the result of the execution cycle of the determination condition is satisfied. When the condition is satisfied, the loop body is executed again; condition is not satisfied, the loop is not executed. Circulating until judgment is performed before entering the loop body to run, it is also known as the test pattern cycle.
Here Insert Picture Description
There are two main Python loop, that is, for statements and while statements. The former is specified in the form of traversal cycle range, which is determined depending on the type of return value of the case and decide whether to perform. To more flexibility to manipulate the flow of circulation, it is necessary to use break, continue and pass other statements.

01 for

for循环是迭代循环,在Python中相当于一个通用的序列迭代器,可以遍历任何有序序列,如str、list、tuple等,也可以遍历任何可迭代对象,如dict。不同于C语言,Python中的for语句将遍历系列中的所有成员,遍历顺序为成员在系列中的顺序。需要注意,在for循环中改变任何序列的内容都是危险的!

for语句不属于当型循环或直到型循环,它遍历序列对象内的元素,对每个元素运行一次循环体,循环的步数在程序开始执行时已经指定,不属于条件判断。

在for语句中,for和in搭配组成for-in循环结构,for-in循环依次把list或tuple中的每个元素迭代出来。for语句的基本语法格式如下:

for 变量 in 序列:
  操作语句

for语句常用的语法格式及其参数说明如下所示:

序列:接收序列,表示遍历范围,无默认值
操作语句:接收操作语句,表示执行一段代码,无默认值

程序的执行从“for变量in序列”开始,该语句把序列中的每个元素代入变量,执行一遍操作语句1,重复的次数就是序列中元素的个数。
为了展示for循环的遍历功能,依次打印list中的姓名,如代码清单1所示。

代码清单1:for语句遍历提取str

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:857662006 寻找有志同道合的小伙伴,互帮互助,
群里还有不错的视频学习教程和PDF电子书!
'''
# 单纯遍历的for语句
names = ['Michael', 'Bob', 'Tracy']
# 遍历输出names中的元素
for name in names:
    print(name)

输出结果:

Michael
Bob
Tracy

for语句同样可以实现dict的遍历方法,如代码清单2所示。

代码清单2:for语句遍历查询dict

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:857662006 寻找有志同道合的小伙伴,互帮互助,
群里还有不错的视频学习教程和PDF电子书!
'''
dic = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
# 遍历键值对
print('key_value:', end = '')
for key, value in dic.items():
    print(key, value, sep = ':', end = ' ')
# 遍历键
print('keys:', end = '')
for key in dic.keys():
    print(key, end = ' ')
# 遍历值
print('values:', end = '')
for value in dic.values():
    print(value, end = ' ')

输出结果:

key_value:a:1 b:2 c:3 d:4
keys:a b c d
values:1 2 3 4

从代码清单2可以看到,for语句中用于遍历的“变量”不仅可以是Python默认的指代词,也可以是常规的变量。

和条件语句一样,循环语句也可以使用嵌套,作用同样是丰富程序的功能性。设计一个成绩录入系统,就必然要录入姓名和课程这两类信息,仅靠一层循环是无法实现的,可使用两层循环结构,如代码清单3所示。

代码清单3:嵌套for语句

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:857662006 寻找有志同道合的小伙伴,互帮互助,
群里还有不错的视频学习教程和PDF电子书!
'''
students = ['一号', '二号']
subjects = ['语文', '数学'] 
sum1 = []
avg = []
for i in students: 
    print ('开始录入%s的考试成绩!'%i) 
    sum = 0
    for j in subjects: 
        print('请输入%s成绩:'%j) 
        score = int(input())
        sum += score
    average = sum / 2
    avg.append(average)
    sum1.append(sum)
print(students, '的总分依次是', sum1, ',', '平均分依次是', avg)
print('完成成绩录入!')

输出结果:

开始录入一号的考试成绩!
请输入语文成绩:
97
请输入数学成绩:
90
开始录入二号的考试成绩!
请输入语文成绩:
89
请输入数学成绩:
100
['一号', '二号'] 的总分依次是 [187, 189] , 平均分依次是 [93.5, 94.5]
完成成绩录入!

理论上,for循环也可以无限嵌套,但并不推荐。

02 while

while语句是Python中最常用的递归结构。区别于for循环,while循环结构包含条件判断式,是一种条件循环,属于当型循环。

while语句最基本的形式包括一个位于顶部的布尔表达式,一个或多个属于while代码块的缩进语句。也可以在结尾处包含一个else代码块,它与while代码块是同级的,组成while-else的形式。while语句的基本语法格式如下。

while 条件表达式:
    操作语句 1
    操作语句 2    

while语句常用的参数及其说明如下所示:

条件表达式:接收布尔表达式,表示判断条件是否成立。无默认值
操作语句:接收操作语句,表示执行一段代码。无默认值

执行while语句时,只要顶部的条件表达式返回真值,就一直执行while部分嵌套的递归代码,当条件表达式返回假值时,不再执行操作语句,程序跳出while结构。

while语句的基础使用方法如代码清单4所示。

代码清单4:while语句

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:857662006 寻找有志同道合的小伙伴,互帮互助,
群里还有不错的视频学习教程和PDF电子书!
'''
sum = 0
n = 99
while n > 0:
    sum += n
    n -= 2
print(sum)

输出结果:2500

如果布尔表达式不带有<、>、==、!=、in、not in等运算符,仅仅给出数值之类的条件,也是可以的。当while后写入一个非零整数时,视为真值,执行循环体;写入0时,视为假值,不执行循环体。也可以写入str、list或任何序列,长度非零则视为真值,执行循环体;否则视为假值,不执行循环体。

如果布尔表达式始终返回1,while语句就变成无限循环,如代码清单5所示。

代码清单5:while语句无限循环

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:857662006 寻找有志同道合的小伙伴,互帮互助,
群里还有不错的视频学习教程和PDF电子书!
'''
# 布尔表达式为常数1,始终为真值
while 1:
     print('循环')

输出结果:

循环
循环
…  
# 布尔表达式每次递归运算都为2,始终为真值
x, y = 2, 1
while x / y:
    print('循环')
    x = x * 2
    y = y * 2

输出结果:

循环
循环
…

运行代码清单5,将会不断打印出“循环”。代码清单5展示了制造无限循环的两种方式,既可以在while后写入一个固定的真值,也可以写入一个一直生成真值的表达式。要终止无限循环,可以使用快捷键Ctrl+C中断循环的执行,也可以用循环终止语句,这将在下文中介绍。

灵活地利用while语句中的布尔表达式及代入表达式的递归值,可以实现特别的功能,如代码清单6所示。

代码清单6:while语句实现str截取

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:857662006 寻找有志同道合的小伙伴,互帮互助,
群里还有不错的视频学习教程和PDF电子书!
'''
string = 'abcd'
while string:
    print(string)
# 该语句的递归计算是,每次从str的第2个字符开始截取
    string = string[1:]

输出结果:

abcd
bcd
cd
d

代码清单6包含一个自减迭代值,它并不通过明显的运算符实现自减,而是利用索引法则,x变量一直从str中第2个值截取至结尾,每次都将位于str最前面的字符截取掉,最终只剩下一个字符时,再次截取就只有空的结果,布尔表达式返回0,循环终止。

通过代码清单5和代码清单6可以看到,灵活地利用递归式,可以实现程序流向的控制。

while循环同样可以使用嵌套,嵌套的while循环实现成绩录入系统如代码清单7所示。

代码清单7:嵌套while语句

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:857662006 寻找有志同道合的小伙伴,互帮互助,
群里还有不错的视频学习教程和PDF电子书!
'''
j = 1
while j <= 2:
    sum = 0
    i = 1
    name = input('请输入学生姓名:')
    while i <= 2:
        print ('请输入第%d门的考试成绩: '%i)
        sum += int(input())
        i += 1
    avg = sum / (i-1)
    print(name, '的平均成绩是%d'%avg)
    j += 1
print('学生成绩输入完成!')    

输出结果:

请输入学生姓名:一号
请输入第1门的考试成绩: 
98
请输入第2门的考试成绩: 
88
一号 的平均成绩是93
请输入学生姓名:二号
请输入第1门的考试成绩: 
65
请输入第2门的考试成绩: 
100
二号 的平均成绩是82
学生成绩输入完成!

代码清单7的第1层while语句用于录入人名,第2层则在各人名下录入多门成绩,布尔表达式决定录入的人数和课程数。

03 break、continue与pass

在前两节中,已经介绍了Python中的两种循环语句。循环语句中还可以嵌入break、continue和pass语句,以灵活地改变流向,实现更多功能。

1. break

在Python中,break语句用于终止循环语句的执行。使用该语句时,即使循环条件判断为真,或序列未被完全递归,循环语句也会被立刻停止。

break语句一般配合条件判断使用,因为程序的终止必须是在某一条件被满足时执行。break语句在for循环和while循环中的使用如代码清单8所示。

代码清单8:break语句的使用

# break语句用于for循环
string = "Python"
for i in string:
# 遍历至string中的字符n时,不再执行else代码块
    if i == 'n':
        break
    else:
        print("letter:{}". format(i))

输出结果:

letter:P
letter:y
letter:t
letter:h
letter:o   
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:857662006 寻找有志同道合的小伙伴,互帮互助,
群里还有不错的视频学习教程和PDF电子书!
'''
# break语句用于while循环
counts = 0
while True:
    print(counts)
    counts += 1
# 满足counts等于3时跳出循环,不再进入循环体
    if counts == 3: 
        break

输出结果:

0
1
2

从代码清单8可以看到,break语句用于for循环和while循环是有区别的。用于for循环时,只终止遍历中某一次的循环体执行;用于while循环时,整个循环被终止。

break只终止本层循环,如有多层嵌套的循环,在其中一层循环中写入break,只在这层循环中生效,程序将跳到上一层循环中继续运行,如代码清单9所示。

代码清单9:break语句用于嵌套循环结构

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:857662006 寻找有志同道合的小伙伴,互帮互助,
群里还有不错的视频学习教程和PDF电子书!
'''
# 第1层循环,遍历次数为2
for i in range(2):
    print("-----%d-----" %i)
# 第2层循环,遍历次数为10
    for j in range(10):
# 使用break语句,j>1时不执行循环体
        if j > 1:
            break
        print(j)

输出结果:

-----0-----
0
1
-----1-----
0
1

在代码清单9中,break语句在条件判断式“if j>1:”后被使用,因此尽管j的指定遍历次数为10,实际上遍历只运行两次。由于break语句只终止本层循环的运行,i依旧遍历执行了两次,而不是在第1次遍历过程末尾终止。

2. continue

Python中的continue语句用于跳出当前循环,并执行下一次循环,而break跳出整层循环,两者的功能具有明显区别。

如果一段代码中包含continue语句,循环执行至continue处时,先忽略本次循环,在本层仍满足条件的剩余循环次数中继续执行,不会终止这一层循环。实际上,如果在某一层的每次循环中都使用continue语句,就相当于使用break语句。

打印一个数表,要不打印某些指定的数字,或只打印某类数,就可以使用continue语句跳过一些循环次数,该语句在for循环和while循环中都可以自由地使用,如代码清单10所示。

代码清单10:continue语句用于循环结构

# 当i等于1或3时,跳过continue后的print语句
for i in range(0,5):
    if i == 1 or i == 3:
        continue
    print(i) 

输出结果:

0
2
4  
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:857662006 寻找有志同道合的小伙伴,互帮互助,
群里还有不错的视频学习教程和PDF电子书!
'''
# 如果遇到偶数,跳过continue后的print语句
counts = 0
while counts < 10:
    counts += 1
    if counts % 2 == 0:
        continue 
    print(counts)

输出结果:

1
3
5
7
9   

break语句一旦用于嵌套循环中的第n层,该层循环会被终止,但在执行第n-1层循环时,仍会创造一个第n层循环并执行。continue语句同样如此,只是仍会执行某一层的剩余部分。因此,无论使用哪种循环终止语句,都只会影响使用终止语句的那一层循环,而不会干扰到其他层。continue语句用于循环的例子如代码清单11所示。

代码清单11:continue语句用于嵌套循环结构

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:857662006 寻找有志同道合的小伙伴,互帮互助,
群里还有不错的视频学习教程和PDF电子书!
'''
# 第1层循环,遍历次数为2
for i in range(2):
    print("-----%d-----" %i)
# 第2层循环,遍历次数为5
    for j in range(5):
# 当j等于2或4时,不执行循环体
        if j == 2 or j == 4:
            continue
        print(j)  

输出结果:

-----0-----
0
1
3
-----1-----
0
1
3

3. pass

pass是空语句,不做任何操作,只起到占位的作用,其作用是为了保持程序结构的完整性。尽管pass语句不做任何操作,但如果暂时不确定要在一个位置放上什么样的代码,可以先放置一个pass语句,让代码可以正常运行。pass语句并非循环或者条件语句的一部分,但与break、continue在代码形式上有些类似。

使用pass语句遍历输出str及数值计算,如代码清单12所示。

代码清单12:pass语句

for element in "Python":
# element为y时,不做任何操作,不会被输出
    if element == "y":
        pass
    else:
        print(element)  

输出结果:

P
t
h
o
n   
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:857662006 寻找有志同道合的小伙伴,互帮互助,
群里还有不错的视频学习教程和PDF电子书!
'''
counts = 0
while counts < 5:
    counts += 1
# i=3时,不执行循环体
    if counts == 3:
        pass
    else:
        print(counts ** 2)    

输出结果:

1
4
16
25 

从代码清单12可以看到,Python在“P”和“t”之间占位,当循环遍历到“y”时不做任何操作;当i等于3时,幂运算不执行,但不影响其他数值。上述两个代码如果在pass的位置缺失,程序将无法执行,因为判断条件没有给出相应的执行语句,会导致逻辑出错。使用pass语句占位,一方面为了让程序正常执行,另一方面也是为了方便以后补充操作语句。

04 列表推导式

推导式是可以从一个数据序列构建另一个新的数据序列的结构体,能够非常简洁地构造新的变量。列表推导式是其中最常用的类型。

列表推导式又称为列表解析式,是Python迭代机制的一种应用,也是一种高效创建列list的方式,可以动态地创建list。由于列表推导式必须用到遍历循环,因此属于一种特殊的循环。

When using a list comprehension, comprehension needs to be written in [] in. elements of the list may be derived from other types of sequence, or sequence iterables meet certain conditions of self. The benefits of using list comprehensions of the code is more concise, to achieve higher efficiency.

The basic syntax list derivation formula as follows:

[操作语句 for变量 in 序列 if 条件表达式]    

List of common parameters and their derivation described below:

  • Manipulation statements: Statement receiving operation, showing implementation of a code. No default
  • Conditional expression: receiving a Boolean expression that determines the condition is satisfied. No default
  • Sequence: receiving a sequence representing traverse range. No default

Formula derivation list may not include conditional expressions, only traversed, generating List, as shown in Listing 13.

Listing 13: deriving formula using a list generated list

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:857662006 寻找有志同道合的小伙伴,互帮互助,
群里还有不错的视频学习教程和PDF电子书!
'''
vec = [-4, -2, 0, 2, 4]
# 用vec中元素的倍数,创建一个数组
print([x * 2 for x in vec])

Output:

[-8, -4, 0, 4, 8]

# 创建一个包含2元tuple的list
print([(x, x ** 2) for x in range(6)]) 

输出结果:

[(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25)]   

In Listing 13, in addition to a simple formula is derived form the list, the list also shows the derivation can be used in various types of functions and variables. Further, the list is derived formulas can also contain conditional statements, as shown in Listing 14.

Listing 14: List comprehensions contain conditional statements

# 过滤list,删除list中的负数
print([x for x in vec if x >= 0])

输出结果:

[0, 2, 4]

The biggest advantage is simple list comprehensions, which needs to be compared with the conventional way of programming. As shown in Listing 15, to create a list a few square composed of two ways here are equivalent, but the list comprehensions way is clear that the code more concise.

Listing 15: Derivation of formula in a conventional manner with Comparative listing

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:857662006 寻找有志同道合的小伙伴,互帮互助,
群里还有不错的视频学习教程和PDF电子书!
'''
# 创建一个平方数list:常规方式
squares = [] 
for x in range(10): 
    squares.append(x ** 2) 
print(squares)

输出结果:

[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
# 创建一个平方数list列表推导式
squares = [x ** 2 for x in range(10)] 
print(squares)    

输出结果:

[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

List comprehension can achieve the same nested loops, as shown in Listing 16.

Listing 16: a list of nested loop comprising derivation formula

# 打印由tuple组成的list,tuple中i由0至2,j由0至2
[(i, j) for i in range(0, 3) for j in range(0, 3)]

输出结果:

[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
# 打印由tuple组成的list,i在0至2之间且小于1,j在0至2之间且大于1
[(i, j) for i in range(0, 3) if i < 1 for j in range(0, 3) if j > 1]

输出结果:

[(0, 2)]

Listing in Listing comprehension 16 in two different list elements integrated together. Wherein the list is derived comprises a pair of brackets, a expression in parentheses, immediately after an expression for a statement, then zero or more for statement or if statement. Calculated by the expression for the statement and if statements as a result of the new list of elements.

Guess you like

Origin blog.51cto.com/14246112/2440124