课后习题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_list
3 [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 1995
3 >>> 

 

(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 >>> things
4 ['mozzarella', 'Cinderella', 'salmonella']

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

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

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

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

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

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

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

1 >>> surprise[-1] = surprise[-1].lower()
2 >>> surprise
3 ['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 >>> e2f
7 {'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 >>> listA
3 [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 >>> animals
10 {'cats': ['Henri', 'Grumpy', 'Lucy'], 'octopi': {}, 'emus': {}}
11 >>> plants={}
12 >>> others={}
13 >>> life = {
14     "animals":animals,
15     "plants":plants,
16     "others":others,
17     }
18 >>> life
19 {'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.cnblogs.com/jingle007/p/11134834.html

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值