Python练习题库
By 红波 2017-12-19
一、填空题
1.Python标准库math中用来计算平方根的函数是__________。(sqrt)
2.在Python中__________表示空类型。(None)
3.列表、元组、字符串是Python的_________(有序?无序)序列。(有序)
4.查看变量类型的Python置函数是________________。(type())
5.查看变量存地址的Python置函数是_________________。(id())
6.表达式[1, 2, 3]*3的执行结果为______________________。([1, 2, 3, 1, 2, 3, 1, 2, 3])
7.list(map(str, [1, 2, 3]))的执行结果为_____________________。([‘1’, ‘2’, ‘3’])
8.已知 x = 3,并且id(x)的返回值为 496103280,那么执行语句 x += 6 之后,表达式 id(x) ==
496103280 的值为___________。(False)
9.已知 x = 3,那么执行语句 x *= 6 之后,x的值为________________。(18)
10.表达式“[3] in [1, 2, 3, 4]”的值为________________。(False)
11.假设列表对象aList的值为[3, 4, 5, 6, 7, 9, 11, 13, 15, 17],那么切片aList[3:7]得到
的值是______________________。([6, 7, 9, 11])
12.使用列表推导式生成包含10个数字5的列表,语句可以写为_______________。([5 for i in
range(10)])
13.假设有列表a = ['name', 'age', 'sex']和b = ['Dong', 38, 'Male'],请使用一个语句将这
两个列表的容转换为字典,并且以列表a中的元素为“键”,以列表b中的元素为“值”,这个语句可以写为_____________________。(c = dict(zip(a, b)))
14.任意长度的Python列表、元组和字符串中最后一个元素的下标为________。(-1)
15.Python语句''.join(list('hello world!'))执行的结果是____________________。('hello
world!')
16.转义字符’\n’的含义是___________________。(回车换行)
17.Python语句list(range(1,10,3))执行结果为___________________。([1, 4, 7])
18.切片操作list(range(6))[::2]执行结果为________________。([0, 2, 4])
19.表达式 'ab' in 'acbed' 的值为________。(False)
20.Python 3.x语句 print(1, 2, 3, sep=':') 的输出结果为____________。(1:2:3)
21.表达式 int(4**0.5) 的值为____________。(2)
22.达式 sorted([111, 2, 33], key=lambda x: -len(str(x))) 的值为____________。([111, 33,
2])
23.已知列表对象x = ['11', '2', '3'],则表达式 max(x) 的值为___________。('3')
24.表达式 min(['11', '2', '3']) 的值为_________________。('11')
25.已知列表对象x = ['11', '2', '3'],则表达式max(x, key=len) 的值为___________。('11')
26.语句 x = (3,) 执行后x的值为_______________。((3,))
27.语句 x = (3) 执行后x的值为________________。(3)
28.已知 x = {1:2},那么执行语句 x[2] = 3之后,x的值为________________。({1: 2, 2: 3})
29.字典对象的_____________方法返回字典中的“键-值对”列表。(items())
30.使用列表推导式得到100以所有能被13整除的数的代码可以写作
___________________________________。([i for i in range(100) if i%13==0])
31.表达式 3 ** 2 的值为_________。(9)
32.表达式 3 * 2的值为___________。(6)
33.已知 x = [3, 5, 7],那么执行语句 x[len(x):] = [1, 2]之后,x的值为______________。([3,
5, 7, 1, 2])
34.表达式 list(zip([1,2], [3,4])) 的值为________________________。([(1, 3), (2, 4)])
35.已知 x = [1, 2, 3, 2, 3],执行语句 x.pop() 之后,x的值为_____________。([1, 2, 3, 2])
36.表达式 [x for x in [1,2,3,4,5] if x<3] 的值为_____________________。([1, 2])
37.表达式 [index for index, value in enumerate([3,5,7,3,7]) if value == max([3,5,7,3,7])]
的值为__________________。([2, 4])
38.已知path = r'c:\test.html',那么表达式path[:-4]+'htm' 的值为__________。
('c:\\test.htm')
39.表达式 '%d,%c' % (65, 65) 的值为________。('65,A')
40.表达式'The first:{1}, the second is {0}'.format(65,97) 的值为
______________________________。('The first:97, the second is 65')
41.表达式 ':'.join('abcdefg'.split('cd')) 的值为______________。('ab:efg')
42.表达式 isinstance('abcdefg', str) 的值为____________。(True)
43.表达式 'Hello world. I like Python.'.find('python') 的值为________。(-1)
44.表达式','.join('a    b ccc\n\n\nddd '.split()) 的值为______________。
('a,b,ccc,ddd')
45.已知 x = '123' 和 y = '456',那么表达式 x + y 的值为______________。('123456')
46.表达式 'abcab'.replace('a','yy') 的值为___________。('yybcyyb')
47.已知 table = ''.maketrans('abcw', 'xyzc'),那么表达式 'Hellow world'.translate(table)
的值为______________________。('Helloc corld')
48.已知x = {'b':1, 'a':2},那么执行语句x.update({‘a’:3, ‘d’:4})之后,表达式
sorted(x.items())的值为____________________。([('a', 3), ('b', 1), ('d', 4)])
49.已知x = list(range(20)),那么语句print(x[100:200])的输出结果为_______________。([])
50.表达式sorted({'a':9, 'b':3, 'c':78}.values())的值为_____________。([3, 9, 78])
二、阅读程序
1.写出下面代码的执行结果。
def Join(List, sep=None):
return (sep or ',').join(List)
print(Join(['a', 'b', 'c']))
print(Join(['a', 'b', 'c'],':'))
答:
a,b,c
a:b:c
2.若k为整数,下述while循环执行的次数为:  9
k=1000
while k>1:
python基础代码练习print(k)
k=k//2
3.写出下面代码的运行结果。
def Sum(a, b=3, c=5):
return sum([a, b, c])
print(Sum(a=8, c=2))
print(Sum(8))
print(Sum(8,2))
答:
13
16
15
4.写出下列程序输出结果
i=1
鼠标手绘图片while i+1:
if i>4:
print("%d"%i)
i+=1
break
print("%d"%i)
i+=1
i+=1
5.写出下面代码的运行结果。
def Sum(*p):
return sum(p)
print(Sum(3, 5, 8))
print(Sum(8))
print(Sum(8, 2, 10))
答:
16
8
20
6.下面程序的执行结果是__________________。(1)s = 0
for i in range(1,101):
s += i
else:
print(1)
7.下面程序的执行结果是______________。(1275)s = 0
for i in range(1,101):
s += i
if  i == 50:
print(s)
break
else:
print(1)
8.阅读下面的代码,输出结果为_____________。
x = list(range(10))
for index, value in enumerate(x):
if value == 3:
x[index] = 5
else:
print(x)
答:将列表x中值为3的元素修改为5。
9.阅读下面的代码,解释其功能:
>>> import string
>>> x = string.ascii_letters + string.digits >>> import random
>>> print(''.join(random.sample(x, 10)))
答:输出由英文字母大小写或数字组成的长度为10且不重复的随机字符串。
10.下面的代码输出结果为_____________。(3)
def demo():
x = 5
x = 3
demo()
print(x)
11.下面程序运行的结果为_______________。([5, 6, 1, 2, 3, 4])
def demo(lst, k):
if k<len(lst):
return lst[k:]+lst[:k]
lst=[1,2,3,4,5,6]
demo(lst,4)
12.下面程序运行的结果为_____ appy New Year!H__________。
def foo(s):
if s=="":
return s
else:
return s[1:]+s[0]
print (foo('Happy New Year!'))
13.下面程序运行的结果是{1:'aa',2:'bb',3:'ff',6:'dd',87: 'ee'}              。
14.下面程序运行的结果为  [3,2] [1,2]                            .
list1=[1,2]
list2=list1[::]
list1[0]=3
print(list1,list2)
smacked
15.下面程序运行的结果为恭喜,你已获得我公司的面试机会!
age, subject,college=(24,“计算机”,”非重点”)
if (age > 25 and subject=="电子信息工程") or (college=="重点" and subject=="电子信息工程" ) or (age<=28 and subject=="计算机"):
print("恭喜,你已获得我公司的面试机会!")
else:
print("抱歉,你未达到面试要求")
16.下面程序运行的结果为
for num in range(2,10):
if  num%2 == 0:
continue
print("Find a odd numer",num)
答案:
Find a odd numer 3
kettle的组成部分Find a odd numer 5
Find a odd numer 7
Find a odd numer 9
17.下面程序打印结果:
for i in range(1, 7):
print(' '*2*(6-i),end='')
for j in range(i, 0, -1):
executioncheck啥意思print(j, end = " ")
print()
18.下面程序输入>>>rev(‘I love you’)返回值为  you love I            def rev1(s):
s=s.split()
s1=‘ ’.join(reversed(s))
return s1
19.阅读程序,打印结果是                  1000
def addInterest(balance,rate):
newBalance=balance*(1+rate)
balance=newBalance
def main():
amount=1000按钮样式调节
rate=0.05
addInterest(amount,rate)
print (amount)
main()
20.阅读程序,打印结果是
def demo(newitem,old_list=[]):
old_list.append(newitem)
return old_list
def main():
print(demo(‘a’))
pr int(demo(‘b’))
main()
答案:['a']
['a', 'b']
21.阅读程序,打印结果是
def func5(a, b, *c):
print(a,b)
print("length of c is %d, c is " %len(c),c)
func5(1,2,3,4,5,6)
答案:1 2
length of c is 4,c is (3, 4, 5, 6)
22.阅读程序,打印结果是 (2.5, 3, 4)
def demo(*para):
avg = sum(para)/len(para)
g = [i for i in para if i>avg]
return (avg,)+tuple(g)
print(demo(1,2,3,4))

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。