之间的元素组成的子列表。例如用户输入[1, 2, 3, 4, 5, 6]和2,5,程序输出[3, 4, 5, 6]。
答:
1)Python 3.4.2代码
x = input('Please input a list:')
x = eval(x)
start, end = eval(input('Please input the start position and the end position:'))
print(x[start:end])
2)Python 2.7.8代码
x = input('Please input a list:')
start, end = input('Please input the start position and the end position:')
print x[start:end]
2.6 列表对象的sort()方法用来对列表元素进行原地排序,该函数返回值为None 。
2.7 列表对象的remove() 方法删除首次出现的指定元素,如果列表中不存在要删除的元素,则抛出异常。
2.8 假设列表对象aList的值为[3, 4, 5, 6, 7, 9, 11, 13, 15, 17],那么切片aList[3:7]得到的值是[6, 7, 9, 11] 。
2.9 设计一个字典,并编写程序,用户输入内容作为“键”,然后输出字典中对应的“值”,如果用户输入的“键”不存在,则输出“您输入的键不存在!”
答:
1)Python 3.4.2代码
d = {1:'a', 2:'b', 3:'c', 4:'d'}
v = input('Please input a key:')
v = eval(v)
print(d.get(v,'您输入的的键不存在'))
2)Python 2.7.8代码
d = {1:'a', 2:'b', 3:'c', 4:'d'}
v = input('Please input a key:')
print(d.get(v,'您输入的的键不存在'))
2.10 编写程序,生成包含20个随机数的列表,然后将前10个元素升序排列,后10个元素降序排列,并输出结果。
答:
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库Python程序设计基础习题复习资料与分析(6)在线全文阅读。
相关推荐: