赞
踩
前言:以下内容仅用于学习记录,如有错误或改进之处,欢迎大家交流学习,共同进步。
将一些朋友的姓名存储在一个列表中 ,并将其命名为names,依次访问该列表中的每个元素,从而将每个朋友的姓名都打印出来。
- names = ["zhangsan","lisi","wangwu"]
- print(names[0])
- print(names[1])
- print(names[2])
'运行
运行结果
继续使用练习3-1中的列表, 但不打印每个朋友的姓名 ,而为每人打印一条消息 ,每条信息都包含相同的问候语 ,但抬头为相应朋友的姓名 。
- names = ["zhangsan","lisi","wangwu"]
- print(f"Hello,{names[0]}!")
- print(f"Hello,{names[1]}!")
- print(f"Hello,{names[2]}!")
'运行
运行结果
想想你喜欢的通勤方式 ,如骑摩托车或开汽车 ,并创建一个包含多种通勤方式的列表 ,根据该列表打印一系列有关这些通勤方式的宣言 ,如:"i would like to own a Honda motorcycle "。
- commute_ways = ["Honda motorcycle","boke","bus","car"]
- print(f"I would like to own a {commute_ways[0]}")
- print(f"I would like to own a {commute_ways[1]}")
- print(f"I would like to own a {commute_ways[2]}")
- print(f"I would like to own a {commute_ways[3]}")
'运行
运行结果
如果你可以邀请任何人一起共进晚餐 (无论是在世的还是故去的) ,你会邀请哪些人 ?请创建一个列表, 其中包含至少三个你想邀请的人 ;然后使用这个列表打印消息,邀请这些人来与你共进晚餐 。
- person = ["dad","mom","sister","brother"]
- print(f"Let's have a dinner together,{person[0]}")
- print(f"Let's have a dinner together,{person[1]}")
- print(f"Let's have a dinner together,{person[2]}")
- print(f"Let's have a dinner together,{person[3]}")
'运行
运行结果
你刚得知一位嘉宾无法赴约 ,因此需要另外邀请一位嘉宾。
①以完成练习3-4时编写的程序为基础 ,在程序末尾添加一条print语句 ,指出哪位嘉宾无法赴约。
②修改嘉宾名单 ,将无法赴约的嘉宾的姓名替换为新邀请的嘉宾的姓名 。
③再次打印一系列消息, 向名单中的每位嘉宾发出邀请 。
- #3-4的基础
- person = ["dad","mom","sister","brother"]
- print(f"Let's have a dinner together,{person[0]}")
- print(f"Let's have a dinner together,{person[1]}")
- print(f"Let's have a dinner together,{person[2]}")
- print(f"Let's have a dinner together,{person[3]}")
-
- #3-5新增
- #实现①
- print(f"{person[0]} can't have a dinner together ")
- #实现②
- person[0]="uncle"
- #实现③
- print(f"Let's have a dinner together,{person[0]}")
- print(f"Let's have a dinner together,{person[1]}")
- print(f"Let's have a dinner together,{person[2]}")
- print(f"Let's have a dinner together,{person[3]}")
'运行
运行结果
你刚找到了一个更大的餐桌 ,可容纳更多的嘉宾。请想想你还想邀请哪三位嘉宾。
①以完成练习3-4或3-5时编写的程序为基础, 在程序末尾添加一条的语句, 指出你找到了一个更大的餐桌。
②使用insert()将一位新嘉宾添加到名单开头。
③使用insert()将另一位新嘉宾添加到名单中间 。
④使用append()将最后一位新嘉宾添加到名单末尾。
⑤打印一系列消息 ,向名单中的每位嘉宾发出邀请 。
- #3-5基础
- person = ["dad","mom","sister","brother"]
- print(f"Let's have a dinner together,{person[0]}")
- print(f"Let's have a dinner together,{person[1]}")
- print(f"Let's have a dinner together,{person[2]}")
- print(f"Let's have a dinner together,{person[3]}")
- print(f"{person[0]} can't have a dinner together ")
- person[0]="uncle"
- print(f"Let's have a dinner together,{person[0]}")
- print(f"Let's have a dinner together,{person[1]}")
- print(f"Let's have a dinner together,{person[2]}")
- print(f"Let's have a dinner together,{person[3]}")
- #完成①
- print("we have a bigger table.")
- #完成②
- person.insert(0,'zhangsan')
- #完成③
- person.insert(3,'lisi')
- #完成④
- person.append('wangwu')
- #完成⑤
- print(f"Let's have a dinner together,{person[0]}")
- print(f"Let's have a dinner together,{person[1]}")
- print(f"Let's have a dinner together,{person[2]}")
- print(f"Let's have a dinner together,{person[3]}")
- print(f"Let's have a dinner together,{person[4]}")
- print(f"Let's have a dinner together,{person[5]}")
- print(f"Let's have a dinner together,{person[6]}")
'运行
运行结果
你刚得知新购买餐桌无法及时送达, 因此只能邀请两位嘉宾。
① 以完成3-6时编写的程序为基础 ,在程序末尾添加一行代码,打印一条你只能邀请两位嘉宾共进晚餐的消息。
②使用pop()不断的删除名单中的嘉宾, 直到只有两位嘉宾为止 。每次从名单中弹出一位嘉宾时,都打印一条消息 ,让该嘉宾知道你很抱歉, 无法邀请他来共进晚餐 。
③对于剩余的两位嘉宾中的每一位,都打印一条消息, 指出他依然在受邀人之列。
④使用del将最后两位嘉宾从名单中删除 ,让名单变成空的, 打印该名单核实程序结束时名单确实是空的。
- # 3-6基础
- person = ["dad","mom","sister","brother"]
- print(f"Let's have a dinner together,{person[0]}")
- print(f"Let's have a dinner together,{person[1]}")
- print(f"Let's have a dinner together,{person[2]}")
- print(f"Let's have a dinner together,{person[3]}")
- print(f"{person[0]} can't have a dinner together ")
- person[0]="uncle"
- print(f"Let's have a dinner together,{person[0]}")
- print(f"Let's have a dinner together,{person[1]}")
- print(f"Let's have a dinner together,{person[2]}")
- print(f"Let's have a dinner together,{person[3]}")
- print("we have a bigger table.")
- person.insert(0,'zhangsan')
- person.insert(3,'lisi')
- person.append('wangwu')
- print(f"Let's have a dinner together,{person[0]}")
- print(f"Let's have a dinner together,{person[1]}")
- print(f"Let's have a dinner together,{person[2]}")
- print(f"Let's have a dinner together,{person[3]}")
- print(f"Let's have a dinner together,{person[4]}")
- print(f"Let's have a dinner together,{person[5]}")
- print(f"Let's have a dinner together,{person[6]}")
-
- #3-7
- #实现①
- print("only two people can have a dinner with me")
- #实现②
- name1 = person.pop()
- print(f"sorry {name1},I can't have a dinner with you. ")
- name2 = person.pop()
- print(f"sorry {name2},I can't have a dinner with you. ")
- name3 = person.pop()
- print(f"sorry {name3},I can't have a dinner with you. ")
- name4 = person.pop()
- print(f"sorry {name4},I can't have a dinner with you. ")
- name5 = person.pop()
- print(f"sorry {name5},I can't have a dinner with you. ")
- #实现③
- print(f"{person[0]},we can have a dinner")
- print(f"{person[1]},we can have a dinner")
- #实现④
- del person[0]
- del person[0]
- print (person)
'运行
运行结果
想出至少五个你渴望去旅游的地方,。
①将这些地方存储在一个列表中, 并确保其中的元素不是按字母顺序排列的。
②按原始排列顺序打印该列表 ,不要考虑输出是否整洁的问题, 只管打印原始Python列表
③使用 sorted ()按字母顺序打印这个列表 同时不要修改它
④再次打印该列表核实排列顺序未变
⑤使用 sorted ()与字母顺序相反的顺序打印这个列表, 同时不要修改它 。
⑥再次打印该列表, 核实排列顺序未变 。
⑦使用reverse()修改列表元素的排列顺序。 打印该列表,核实列表顺序确实变了。
⑧使用reverse()再次修改列表元素的排列顺序。 打印该列表,核实已恢复到原来的排列顺序。
⑨使用sort()修改该列表,使其元素按字母顺序排列,打印该列表,核实列表顺序确实变了。
⑩使用sort()修改该列表,表示其元素按字母顺序相反的顺序排列,打印该列表,核实排列顺序确实变了。
- #①
- position = ["beijing","shanghai","xian","jiangsu","guangzhou"]
- #②
- print(position)
- #③
- print(sorted(position))
- #④
- print(position)
- #⑤
- print(sorted(position,reverse=True))
- #⑥
- print(position)
- #⑦
- position.reverse()
- print(position)
- #⑧
- position.reverse()
- print(position)
- #⑨
- position.sort()
- print(position)
- #⑩
- position.sort(reverse=True)
- print(position)
'运行
运行结果
在完成练习3-4 ~练习3-7时编写的程序之一中,使用len()打印一条消息,指出你邀请了多少位嘉宾来与你共进晚餐 。
- # 3-4
- person = ["dad","mom","sister","brother"]
- print(f"Let's have a dinner together,{person[0]}")
- print(f"Let's have a dinner together,{person[1]}")
- print(f"Let's have a dinner together,{person[2]}")
- print(f"Let's have a dinner together,{person[3]}")
- print(len(person))
'运行
运行结果
想想可存储到列表的东西 ,如山岳、 河流 、国家、 城市、语言或你喜欢的任何东西。编写一个程序 ,在其中创建一个包含这些元素的列表, 然后,对于本章介绍的每个函数,都至少使用一次来处理这个列表 。(上面3-9基本都涉及到,这里汇总以下本章的主要函数)
#本章涉及的函数:
#列表末尾增加元素:append()
#列表中插入元素:insert()
#列表中删除元素:del 需要知道索引
#列表中删除元素并继续使用:pop()
#根据值删除元素:remove() 只能删除第一个,有多个时需要循环
#永久性排序:sort()reverse=True时表示倒序
#临时性排序:sorted() reverse=True时表示倒序
#倒着打印:reverse()
#确定长度:len()
如果你还没有在程序中遇到索引错误, 就尝试引发一个这种错误 ,在你的一个程序中修改其中的索引,以引发索引错误 ,关闭程序前务必消除这个错误 。
解释:下面的程序是3-4的例题,这里的索引号最大为3。若使用person[4]就会引发错误 。
- #3-4
- person = ["dad","mom","sister","brother"]
- print(f"Let's have a dinner together,{person[0]}")
- print(f"Let's have a dinner together,{person[1]}")
- print(f"Let's have a dinner together,{person[2]}")
- print(f"Let's have a dinner together,{person[3]}")
'运行
本章结束啦~~~
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。