打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
课后习题1

(1) 创建一个叫作years_list 的列表,存储从你出生的那一年到五岁那一年的年份。例
如,如果你是1980 年出生的,那么你的列表应该是years_list = [1980, 1981, 1982,
1983, 1984, 1985]。
如果你现在还没到五岁却在阅读本书,那我真的没有什么可教你的了。


1 >>> My_year_list = [1990,1991,1992,1993,1994,1995]2 >>> My_year_list3 [1990, 1991, 1992, 1993, 1994, 1995]

 

(2) 在years_list 中,哪一年是你三岁生日那年?别忘了,你出生的第一年算0 岁。

1 >>> My_year_list[3]2 1993

 

(3) 在years_list 中,哪一年你的年纪最大?

 

1 >>> My_year_list[-1]2 19953 >>> 

 

(4) 创建一个名为things 的列表, 包含以下三个元素:"mozzarella"、"cinderella" 和
"salmonella"。

>>> things = [ 'mozzarella', 'cinderella' , 'salmonella']>>> things['mozzarella', 'cinderella', 'salmonella']

 

(5) 将things 中代表人名的字符串变成首字母大写形式,并打印整个列表。看看列表中的

元素改变了吗?

1 >>> things = [ 'mozzarella', 'cinderella' , 'salmonella']2 >>> things[things.index("cinderella")] = things[things.index("cinderella")].capitalize()3 >>> things4 ['mozzarella', 'Cinderella', 'salmonella']

(6) 将things 中代表奶酪的元素全部改成大写,并打印整个列表。

1 >>> things[things.index("mozzarella")] = things[things.index("mozzarella")].upper()2 >>> things3 ['MOZZARELLA', 'Cinderella', 'salmonella']4 >>> 

(7) 将代表疾病的元素从things 中删除,收好你得到的诺贝尔奖,并打印列表。

1 >>> things.remove("salmonella")2 >>> things3 ['MOZZARELLA', 'Cinderella']4 >>> 

(8) 创建一个名为surprise 的列表,包含以下三个元素:"Groucho"、"Chico" 和"Harpo"。

1 >>> surprise = [ 'Groucho','Chico','Harpo' ]2 >>> surprise3 ['Groucho', 'Chico', 'Harpo']4 >>> 

(9) 将surprise 列表的最后一个元素变成小写,翻转过来,再将首字母变成大写。

1 >>> surprise[-1] = surprise[-1].lower()2 >>> surprise3 ['Groucho', 'Chico', 'harpo']4 >>> 

(10) 创建一个名为e2f 的英法字典并打印出来。这里提供一些单词对:dog 是chien,cat

是chat,walrus 是morse。

1 >>> e2f = {2     "dog":"chien",3     "cat":"chat",4     "walrus":"morse"5     }6 >>> e2f7 {'dog': 'chien', 'cat': 'chat', 'walrus': 'morse'}

(11) 使用你的仅包含三个词的字典e2f 查询并打印出walrus 对应的的法语词。

1 >>> e2f["walrus"]2 'morse'3 >>> 

(12) 利用e2f 创建一个名为f2e 的法英字典。注意要使用items 方法。

  

1 f2e = { value:key for key,value in e2f.items() }
>>> f2e{'chien': 'dog', 'chat': 'cat', 'morse': 'walrus'}

 

(13) 使用f2e,查询并打印法语词chien 对应的英文词。

1 >>> f2e["chien"]2 'dog'

 

(14) 创建并打印由e2f 的键组成的英语单词集合。

1 >>> listA = [ e2f.keys() ]2 >>> listA3 [dict_keys(['dog', 'cat', 'walrus'])]

(15) 建立一个名为life 的多级字典。将下面这些字符串作为顶级键:'animals'、'plants'

以及'others'。令'animals' 键指向另一个字典,这个字典包含键'cats'、'octopi'
以及'emus'。令'cat' 键指向一个字符串列表,这个列表包括'Henri'、'Grumpy' 和
'Lucy'。让其余的键都指向空字典。

 1 >>> cat = [ 'Henri','Grumpy','Lucy' ] 2 >>> octopi = {} 3 >>> emus = {} 4 >>> animals = { 5     "cats":cat, 6     "octopi":octopi, 7     "emus":emus, 8     } 9 >>> animals10 {'cats': ['Henri', 'Grumpy', 'Lucy'], 'octopi': {}, 'emus': {}}11 >>> plants={}12 >>> others={}13 >>> life = {14     "animals":animals,15     "plants":plants,16     "others":others,17     }18 >>> life19 {'animals': {'cats': ['Henri', 'Grumpy', 'Lucy'], 'octopi': {}, 'emus': {}}, 'plants': {}, 'others': {}}20 >>> print(life)21 {'animals': {'cats': ['Henri', 'Grumpy', 'Lucy'], 'octopi': {}, 'emus': {}}, 'plants': {}, 'others': {}}22 >>> 

(16) 打印life 的顶级键。

1 >>> life.keys()2 dict_keys(['animals', 'plants', 'others'])

 

(17) 打印life['animals'] 的全部键。

1 >>> life["animals"].keys()2 dict_keys(['cats', 'octopi', 'emus'])3 >>> 

 

(18) 打印life['animals']['cats'] 的值。

1 >>> life["animals"]["cats"]2 ['Henri', 'Grumpy', 'Lucy']3 >>> print(life["animals"]["cats"])4 ['Henri', 'Grumpy', 'Lucy']5 >>> 

 

来源:https://www.icode9.com/content-4-330701.html
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
cinderella。
2021北京一零一中初二(上)期中英语(教师版)
灰姑娘(Cinderella)
八年级英语(4册1-4单元)阶段性评价试题
Alice in Wonderland(CHAPTER 2)The Pool of Tears
九年级英语上册期末复习卷
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服