题库
一、 简答题 (共33题,165分)
1、要求:一个内容为0,88,ac,ff,abc,ss的元组。
tuple=(0,88,"ac","ff","abc","ss")
2、删除元素ff
3、并在位置4添加新的元素h
4、返回新的元组。
(5.0)
正确答案:
解析:
2、1、创建一个列表:colors,内部元素为:red,yellow,blue,white,black,green,gray,
2、在black前面插入一个元素:orange
3、把orange改为橙色。
4、在blue后面插入一个子表:[“pink”,”purple”]
5、返回white的索引值。
6、创建一个新列表11,22,33,44,并合并到colors中。
7、取出索引值4-8的所有元素。
8、取出最后2个元素。
9、使用循环,遍历列表所有元素,并输出元素值和对应的索引值。
10、删除gray元素。
(5.0)
正确答案:# 1、创建一个列表:colors,内部元素为:red,yellow,blue,white,black,green,gray
colors = ['red','yellow','blue','white','black','green','gray']
print('第一题:',colors)
# 2、在black前面插入一个元素:orange
colors.insert(4,'orange')
print('第二题:',colors)
# 3、把orange改为橙色。
colors[4] = '橙色'
print('第三题:',colors)
# 4、在blue后面插入一个子表:[“pink”,”purple”]
list = ['pink','purple']
colors.insert(3,list)
print('第四题:',colors)
# 5、返回white的索引值。
print('第五题:',colors.index('white'))
# 6、创建一个新列表11,22,33,44,并合并到colors中。
newlist= [11,22,33,44,]
print('第六题:',colors + nub)
# 7、取出索引值4-8的所有元素。
print('第七题:',colors[4:8])
# 8、取出最后2个元素。
print('第八题:',colors[-2:])
# 9、使用循环,遍历列表所有元素,并输出元素值和对应的索引值。
for i in colors:
print('第九题:','元素值是:',i,'索引值是:',colors.index(i))
#10、删除gray元素。
# del colors[8] # 方法1
# colors.pop(8) # 方法2
colors.remove("gray") # 方法3
#11、删除colors。
del colors
解析:
3、 使用列表推导式生成一个存放1-100中个位数为3的数据列表。
(5.0)
正确答案: result = [x for x in range(3, 101, 10)]
print(result)
解析:
4、补充下列代码,实现输入一个8位数表示的年月日,读出月份数字并输出该月有多少天。
def is_leap(year):
"""判断year是否为闰年,闰年返回True,非闰年返回False"""
#####
def days_of_month(date_str):
"""根据输入的年月日,返回该月的天数"""
#####
date_in = input() # 输入一个年月日
print(days_of_month(date_in))
(5.0)
正确答案:
解析:
5、使用递归法计算自然数各位数字之和。
比如:输入151,经递归函数计算后输出1+5+1计算结果:7
通常要获得数字的最后一位数,可以取模:
last= n % 10
该数字的其余部分(不包括最后一个地方)是:
rest = n // 10
(5.0)
正确答案:def func(num):
if num < 10:
return num
else:
return func(num // 10) + func(num % 10)
num = int(input("请输入任意数字: "))
res = func(num)
print(res)
解析:
6、使用map()函数计算自然数各位数字之和。
(5.0)
正确答案:
n=input()
l=list(map(int,n))
print(sum(l))
解析:
7、使用三种方法调用math库中的pow(底数,指数)函数,计算2的100次方。
&