Python编程二级 题库
共 6 道题
· 按题型筛选,点「查看答案」核对解析
2
单选题
适中
执行以下代码后输出的结果是?( )
A
{1: 'monkey', 2: 'panda', 4: 'fish', 5: 'sheep', 3: 'cat'}
B
{1: 'monkey', 2: 'panda', 4: 'fish', 5: 'sheep', 3: 'dog'}
C
{1: 'monkey', 2: 'panda', 3: 'dog, 5: 'sheep', 4: 'duck'}
D
{1: 'monkey', 2: 'panda', 3: 'cat', 5: 'sheep', 4: 'duck'}
答案与解析
正确答案:B
d[5]='sheep' #{1: 'monkey', 2: 'panda', 3: 'bird', 4: 'fish', 5: 'sheep'} del d[3] #{1: 'monkey', 2: 'panda', 4: 'fish', 5: 'sheep'} d[3]='dog'#{1: 'monkey', 2: 'panda', 3: 'dog', 5: 'sheep'} d[3]='dog'#{1: 'monkey', 2: 'panda', 4: 'fish', 5: 'sheep', 3: 'dog'}
3
单选题
适中
已知列表animal = ['cat','dog','monkey','tiger','lion'],要想得到'cat'的输出结果,以下哪个选项的语句是不正确的?( )
A
animal[0]
B
animal[1]
C
animal.reverse()
animal[4]
D
animal.reverse()
animal[-1]
答案与解析
正确答案:B
列表元素的索引是从0开始的,所以获取第一个元素’cat’需用animal[0]而不是animal[1];reverse()方法的作用是将列表中的元素反转,animal列表反转后,第一个元素’cat’变成了最后一个元素,可以通过索引值4或者逆序索引值-1进行访问。所以本题答案为B。
5
判断题
偏易
字典(dictionary)和列表(list)的共同点是都可以通过下标访问内容。( )
A
对
B
错
答案与解析
正确答案:错
考察字典的概念,字典存储的是键值对,只能通过键值访问。
6
判断题
偏易
字典的访问方式是通过键来获取值,而不能通过值获取键。( )
A
对
B
错
答案与解析
正确答案:对
字典的核心结构是键值对,键是唯一的,值可以重复,所以Python 中字典的常规访问方式是通过键获取值,无法直接通过值获取对应的键。