
本文共 3726 字,大约阅读时间需要 12 分钟。
笔记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 listfor city in cities: print(f"This is a large city {cities}:")# same as above for loop personsfor 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 itfor i in pairs: print(f"I get {i}")# we can also build lists, first start with an empty oneempty_elements = []# then use the range function to do 5 to 10 countsfor 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 toofor 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 = 1numbers = []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> 0begin to whileAt the top i is 1Numbers now: [1]At the bottom i is 2At the top i is 2Numbers now: [1, 2]At the bottom i is 3At the top i is 3Numbers now: [1, 2, 3]At the bottom i is 4At the top i is 4Numbers now: [1, 2, 3, 4]At the bottom i is 5At the top i is 5Numbers now: [1, 2, 3, 4, 5]At the bottom i is 6The numbers:12345
单纯留下while指令形成ex38.2.py,也运行顺利。
练习ex38.2.py
在这里插入代码片zhang = input("> ")if zhang == "0": print("begin to while")i = 1numbers = []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> 0begin to whileAt the top i is 1Numbers now: [1]At the bottom i is 2At the top i is 2Numbers now: [1, 2]At the bottom i is 3At the top i is 3Numbers now: [1, 2, 3]At the bottom i is 4At the top i is 4Numbers now: [1, 2, 3, 4]At the bottom i is 5At the top i is 5Numbers now: [1, 2, 3, 4, 5]At the bottom i is 6The numbers:PS C:\Users\lenovo\1pythonw>
两者的简单比较:
这两种循环都是把列表中的元素用同样的操作进行一次,但如同在练习33中所看到的,while循环有可能无法终止,一直进行,for好像没有这个问题。而且就我的理解,你把for循环可以并列列出多个也容易操作运行,但如果while的指令似乎就不能够做到这样。
继续看这个ex38练习。
发表评论
最新留言
关于作者
