笔记23 笨办法习题32/33 循环与列表
这两个练习,在列表的基础上做循环。好像那个for是在体现循环,后一个练习的while,也是在体现循环。列表好像不难理解,方括号[ ]中的数字或者符号串,排列所构成的东西。列表list中的东西,大概都可以是某个变量的值。
我们再看for。它为何就表示着循环的涵义呢?看ex32中的行6,用for来调用变元the_count中的每一个数,行7用f函数就在ps中体现出来。我猜测,因为每个同样的动作都执行一次,有几个数就执行几次,似乎就是这个动作的循环。水果那个列表也是如此。就此而言,这个for指令,就是把同样的操作在执行平台上反复地做下去,一直做到指令所要求的重复数为止。
第三个for还另有点意思,它是对一个混合性列表的提取,那个i,既可以表示变元change中的第一个数字,也可以表示其中的第一个字符串。只要是对应位置就行,就可以提取。依然是同样的动作按要求不断地重复,因此,所谓循环,和执行的重复进行是一个涵义。
Range函数显然是指的列表范围,按照巩固练习2的要求,试了好多次,好像都不行,如果去掉循环,一次一次地做,估计代码会很长。暂且思考到这里吧。我查了一下列表的涵义,复制如下:
函数range`
class range(object)
| range(stop) -> range object
| range(start, stop[, step]) -> range object
|
| Return an object that produces a sequence of integers from start (inclusive)
| to stop (exclusive) by step. range(i, j) produces i, i+1, i+2, …, j-1.
| start defaults to 0, and stop is omitted! range(4) produces 0, 1, 2, 3.
| These are exactly the valid indices for a list of 4 elements.
| When step is given, it specifies the increment (or decrement).
大意应该是,range是一个内置函数中的对象,它会从其包括的起点到终点的数字中,返回起点数字和随后的数字,但不包含终点数字。若起点缺省,则为数字0.,当一个列表的步长确定时,最后的数字则不返回。
练习33也称为循环,while的循环和for的循环有什么区别呢?似乎是,for针对的是词项,例如练习33中行15的for num in numbers:显然是一排数字。While则是一个类似于条件的东西,ex33中的行4,while I < 6,显然是一个条件。只有在这个条件取值为真的时候,其后的代码才会如同for一样循环重复地执行。
做巩固练习,我把while换成for,结果代码执行后停不下来。
练习ex33.1.py
在这里插入代码片i = 0
numbers = [0, 1, 2, 3, 4, 5,6]
for i in numbers:
print(f"At the top i is {i}")
numbers.append(i)
i = i + 1
print("Numbers now: ", numbers)
print(f"At the bottom i is {i}")
print("The numbers: ")
for num in numbers:
print(num)
执行结果,一直执行不停,按ctrol v才停。复制小片段。
在这里插入代码片, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3
5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3,
5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3,
, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4
6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4,
6, 0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 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2,
4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2,
换一个函数,用if替代while,做成ex33.2.py,也可以运行,但没有循环的功能了,有点意思。
练习3x33.2.py
在这里插入代码片i = 0
numbers = [0, 1, 2, 3, 4, 5]
def sixnum(numbers, i) :
print(f"At the top i is {i}")
numbers.append(i)
i = i + 1
print("Numbers now: ", numbers)
print(f"At the bottom i is {i}")
print("The numbers: ")
for num in numbers:
print(num)
执行结果,前部分无效,后部分出现
在这里插入代码片PS C:\Users\lenovo> cd 1pythonw
PS C:\Users\lenovo\1pythonw> pytho
The numbers:
0
1
2
3
4
5
PS C:\Users\lenovo\1pythonw>
再换一个函数,用for替代while,然后也引入range函数、则有
练习ex33.3.py
在这里插入代码片i = 0
numbers = []
for i in range(0, 6) :
print(f"At the top i is {i}")
numbers.append(i)
i = i + 1
print("Numbers now: ", numbers)
print(f"At the bottom i is {i}")
print("The numbers: ")
for num in numbers:
print(num)
执行成功。
在这里插入代码片PS C:\Users\lenovo\1pythonw> python ex33.3.py
At the top i is 0
Numbers now: [0]
At the bottom i is 1
At the top i is 1
Numbers now: [0, 1]
At the bottom i is 2
At the top i is 2
Numbers now: [0, 1, 2]
At the bottom i is 3
At the top i is 3
Numbers now: [0, 1, 2, 3]
At the bottom i is 4
At the top i is 4
Numbers now: [0, 1, 2, 3, 4]
At the bottom i is 5
At the top i is 5
Numbers now: [0, 1, 2, 3, 4, 5]
At the bottom i is 6
The numbers:
0
1
2
3
4
5
PS C:\Users\lenovo\1pythonw>
在ps中执行成功,结果一样,值得人思考。