笔记29 笨办法学python练习38 列表前的for与while简略比较
这个习题38,在讲述列表操作之前,特地提到了两个条件操作符号,一个是while,另一个是for。这两个仍然是让我感到困惑的,先温习温习,再来看列表吧。
While是一个有点难以理解的概念,它和for一样,也被称为循环指令。但这个循环的条件是,循环语句中的布尔值为true。它和for的区别何在呢?
相同之处大概在于循环,循环的对象常常就是一个列表中陈列的对象,所以这两个指令都和列表关系密切。还是把书中的例题稍加改造,在本文中先对比一下它们,然后概括出两者间的简单区别吧。
使用for-loop的习题ex38.0.py
在这里插入代码片cities = ['guangzhou', 'wuhan', 'shenzhen', 'nanjing', 'shenyang']
persons = ['zhang', 'wang', 'liu', 'li', 'chen']
pairs = ['guangzhou', 'one', 'wuhan', 'second', 'shenzhen', 'third', 'nanjing', 'forth']
# this first kind for-loop goes through a city list
for city in cities:
print(f"This is a large city {cities}:")
# same as above for loop persons
for name in persons:
print(f"A name of somebody: {persons}")
# also we can go through mixed lists too, one to one
# notice we have to use {} since we don't know what's in it
for i in pairs:
print(f"I get {i}")
# we can also build lists, first start with an empty one
empty_elements = []
# then use the range function to do 5 to 10 counts
for i in range(5, 10):
print(f"Adding {i} to the list.")
# append is a function that lists Understand
empty_elements.append(i)
# now we can print them out too
for i in empty_elements:
print(f"Element was: {i}")
运行结果在这里插入代码片
PS C:\Users\lenovo> cd 1pythonw
PS C:\Users\lenovo\1pythonw> python ex38.0.py
This is a large city [‘guangzhou’, ‘wuhan’, ‘shenzhen’,
This is a large city [‘guangzhou’, ‘wuhan’, ‘shenzhen’,
This is a large city [‘guangzhou’, ‘wuhan’, ‘shenzhen’,
This is a large city [‘guangzhou’, ‘wuhan’, ‘shenzhen’,
This is a large city [‘guangzhou’, ‘wuhan’, ‘shenzhen’,
A name of somebody: [‘zhang’, ‘wang’, ‘liu’, ‘li’, 'chen
A name of somebody: [‘zhang’, ‘wang’, ‘liu’, ‘li’, 'chen
A name of somebody: [‘zhang’, ‘wang’, ‘liu’, ‘li’, 'chen
A name of somebody: [‘zhang’, ‘wang’, ‘liu’, ‘li’, 'chen
A name of somebody: [‘zhang’, ‘wang’, ‘liu’, ‘li’, 'chen
I get guangzhou
I get one
I get wuhan
I get second
I get shenzhen
I get third
I get nanjing
I get forth
Adding 5 to the list.
Adding 6 to the list.
Adding 7 to the list.
Adding 8 to the list.
Adding 9 to the list.
Element was: 5
Element was: 6
Element was: 7
Element was: 8
Element was: 9
使用while的习题ex38.1.py
在这里插入代码片zhang = input("> ")
if zhang == "0":
print("begin to while")
i = 1
numbers = []
while i < 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 ex38.1.py
> 0
begin to while
At the top i is 1
Numbers now: [1]
At the bottom i is 2
At the top i is 2
Numbers now: [1, 2]
At the bottom i is 3
At the top i is 3
Numbers now: [1, 2, 3]
At the bottom i is 4
At the top i is 4
Numbers now: [1, 2, 3, 4]
At the bottom i is 5
At the top i is 5
Numbers now: [1, 2, 3, 4, 5]
At the bottom i is 6
The numbers:
1
2
3
4
5
单纯留下while指令形成ex38.2.py,也运行顺利。
练习ex38.2.py
在这里插入代码片zhang = input("> ")
if zhang == "0":
print("begin to while")
i = 1
numbers = []
while i < 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: ")
运行结果
在这里插入代码片PS C:\Users\lenovo\1pythonw> python ex38.2.py
> 0
begin to while
At the top i is 1
Numbers now: [1]
At the bottom i is 2
At the top i is 2
Numbers now: [1, 2]
At the bottom i is 3
At the top i is 3
Numbers now: [1, 2, 3]
At the bottom i is 4
At the top i is 4
Numbers now: [1, 2, 3, 4]
At the bottom i is 5
At the top i is 5
Numbers now: [1, 2, 3, 4, 5]
At the bottom i is 6
The numbers:
PS C:\Users\lenovo\1pythonw>
两者的简单比较:
这两种循环都是把列表中的元素用同样的操作进行一次,但如同在练习33中所看到的,while循环有可能无法终止,一直进行,for好像没有这个问题。而且就我的理解,你把for循环可以并列列出多个也容易操作运行,但如果while的指令似乎就不能够做到这样。
继续看这个ex38练习。