注,以下答案仅为个人所做,
不代表标准答案
真题word版需要的话可以评论个邮箱
单选
前五道题在2019年真题时也出现了(毕竟是自命题,可能老师也懒得编新花样),所以是可能有机会做到原题的
1、列表a=[1],b=[2,3],执行a. extend(b)后,a的值为 ( )
A、[1,2,3] B、[1,[2,3]] C、[1,2] D、以上都不对
a=[1]
b=[2,3]
a.extend(b)
print("answer_1:"+str(a))#[1,2,3]
如果此题将extend变成append,答案将会是B。
append将列表当做一个元素,extend将列表内的元素当成元素
2、a=“xiaozhupeiqi” print a.find(“z”)的结果: ( )
A、5 B、4 C、-1 D、以上都不对
a="xiaozhupeiqi"
print("answer_2:"+str(a.find("z")))#4
find函数找出第一个符合条件的字符(串),并返回下标
注意下标从0开始数起
3、 a=0
while a<10:
a=a+3
print a
结果为 ( )
A、10 B、9 C、11 D、12
a=0
while a<10:
a=a+3
print("answer_3:"+str(a))#12
易错选B,while循环运行4次,第三次运行完毕后a=9,可以进入第4次循环,a=12,不满足进入循环的条件,循环结束
3、a=“1:5,1:4”,问a的变量类型? ( )
A、列表 B、字典 C、字符串 D、整数
注:此题序号真题卷有错误
a="1:5,1:4"
print("answer_3:"+str(type(a)))#<class 'str'>
4、下面哪些是合法变量名? ( )
A、def B、def1 C、1def D、”def”
变量名要以字母开头,且不能和保留字冲突
5、s= ‘xiao\nzhu\tpei’ print len(s) ( )
A、12 B、13 C、14 D、15
s= 'xiao\nzhu\tpei'
print("answer_5:"+str(len(s)))#12
\n表示换行符,\t表示tab键,都是一个字符,其中’'是作为转义字符出现
6、print “%s**%s”%(2,3) ( )
A、 %s**%s B、8 C、2**3 D、2s* *3s
print("answer_6:"+"%s**%s"%(2,3))#2**3
将2和3插入到%s占位符的字符串中,就形成了字符串’2**3’,
注意此处不要把2**3当做23计算出来,他们是以字符串存在的
7、以下哪一个是以添加的方式打开文件 ( )
A、f=open(r"c:\file.txt’,’r’) B、f=open(‘file.txt’,’a’)
C、f=open(r"c:\file.txt’,’w’) D、以上都不是
详解可见:https://blog.csdn.net/laobai1015/article/details/91957436
8、for i in range(1,7,2):
print i
问循环的执行次数: ( )
A、 3 B、4 C、7 D、6
cnt=0
for i in range(1,7,2):
#print(i)
cnt+=1
print("answer_8:"+str(cnt))#3
取1,3,5,不取7
9、 a=2
if a<2:
print 1
elif a<3:
print 2
else:
print 3
问输出结果: ( )
A、 1 B、2 C、3 D、None
a=2
if a<2:
print("answer_9:"+str(1))
elif a<3:
print("answer_9:"+str(2))
else:
print("answer_9:"+str(3))
#2
10、def xy(a1):
c=[]
for i in a1:
if i not in c:
c.append(i)
return c
print xy([2,1,5,1,2,3])
输出结果为 ( )
A、[2,1,5,1,2,3] B、5 C、[2, 1, 5, 3] D、以上都不是
注:题中有缩进
def xy(a1):
c = []
for i in a1:
if i not in c:
c.append(i)
return c
print("answer_10:"+str(xy([2, 1, 5, 1, 2, 3])))#[2, 1, 5, 3]
这个函数的意思就是将第一次出现的数组成一个新列表
12、列表a=[2,3],b=[1,3],执行b=b+a后,b的值为: ( )
A、 [2,3,1] B、[1,3] C、[1,3,2,3] D、[[1,3],[2,3]]
a=[2,3]
b=[1,3]
b=b+a
print("answer_12:"+str(b))#[1, 3, 2, 3]
注意不是D,列表加法是元素相加
13、a=3
b=4
c=b
b=a
a=c
print a,b 输出结果为: ( )
A、 4,3 B、3,4 C、4,4 D、3,3
a=3
b=4
c=b
b=a
a=c
print(f"answer_13:{a},{b}")#4,3
14、x=len([3,2,[1,2,3]]),x的值是: ( )
A、2 B、3 C、4 D、5
x=len([3,2,[1,2,3]])
print(f"answer_14:{x}")#3
[1,2,3]是作为列表的单独一个元素存在的
15、设s = “hello world”, 则s[3:8]的值为 ( )
A、”lo wor” B、”llowo” C、”llo w” D、”lo wo”
s='hello world'
print(f"answer_15:{s[3:8]}")#lo wo
注意下标从0开始,不要忽略了空格即可
填空
1、 print “a” in “abc” 输出结果:____________
print("a" in "abc")#True
in函数输出布尔值
2、 x=“123”
print x[1] 输出结果:____________
print x*2 输出结果:____________
x="123"
print(x[1])#2
print(x*2)#123123
3、 db={‘i’:3, ‘k’:5}
print db.keys() 输出结果:____________
print db.items() 输出结果:____________
db={'i':3,'k':5}
print (db.keys())#dict_keys(['i', 'k'])
print (db.items())#dict_items([('i', 3), ('k', 5)])
- 正确答案:dict_keys([‘i’, ‘k’]) dict_items([(‘i’, 3), (‘k’, 5)])
保险起见,不要漏了前面的英文字母
4、 b=[]
for i in range(1,4):
____________
print b
输出结果:[1,4,9]
b=[]
for i in range(1,4):
b.append(i**2)
print(b)#[1, 4, 9]
遍历i=1,2,3,目标是1,4,9的列表,自然就能想到append和平方
5、b=“chengxusheji”
print b.split(“h”) 输出结果:____________
print b.replace(“e”,"") 输出结果:____________
b="chengxusheji"
print(b.split("h"))#['c', 'engxus', 'eji']
print(b.replace("e",""))#chngxushji
- 正确答案:[‘c’, ‘engxus’, ‘eji’] chngxushji
以h分隔,组建一个列表
用空字符串代替e,返回一个字符串(注意不要写’’,字符串输出不带引号)
6、a=[2,3,1,4,5]
print __________ 输出结果:15
a=[2,3,1,4,5]
print(sum(a))#15
求和
7、a=[2,1,4,2,2,4,5]
b=0
for i in a:
if i==2:
__________
print b
输出结果:3
a=[2,1,4,2,2,4,5]
b=0
for i in a:
if i==2:
b=b+1
print(b)#3
数列表里有几个2
全部代码
#1.列表a=[1],b=[2,3],执行a. extend(b)后,a的值为
a=[1]
b=[2,3]
a.extend(b)
print("answer_1:"+str(a))#[1,2,3]
#2.a="xiaozhupeiqi" print a.find("z")的结果
a="xiaozhupeiqi"
print("answer_2:"+str(a.find("z")))#4
#3.
a=0
while a<10:
a=a+3
print("answer_3:"+str(a))#12
#3.a="1:5,1:4",问a的变量类型?
a="1:5,1:4"
print("answer_3:"+str(type(a)))#<class 'str'>
#5.
s= 'xiao\nzhu\tpei'
print("answer_5:"+str(len(s)))#12
#6.
print("answer_6:"+"%s**%s"%(2,3))#2**3
#8.
cnt=0
for i in range(1,7,2):
#print(i)
cnt+=1
print("answer_8:"+str(cnt))#3
#9.
a=2
if a<2:
print("answer_9:"+str(1))
elif a<3:
print("answer_9:"+str(2))
else:
print("answer_9:"+str(3))
#2
#10.
def xy(a1):
c = []
for i in a1:
if i not in c:
c.append(i)
return c
print("answer_10:"+str(xy([2, 1, 5, 1, 2, 3])))#[2, 1, 5, 3]
#12.列表a=[2,3],b=[1,3],执行b=b+a后,b的值为:
a=[2,3]
b=[1,3]
b=b+a
print("answer_12:"+str(b))#[1, 3, 2, 3]
#13.
a=3
b=4
c=b
b=a
a=c
print(f"answer_13:{a},{b}")#4,3
#14.x=len([3,2,[1,2,3]]),x的值是
x=len([3,2,[1,2,3]])
print(f"answer_14:{x}")#3
#15.设s = “hello world”, 则s[3:8]的值为
s='hello world'
print(f"answer_15:{s[3:8]}")#lo wo
#1
print("a" in "abc")#True
#2
x="123"
print(x[1])#2
print(x*2)#123123
#3
db={'i':3,'k':5}
print (db.keys())#dict_keys(['i', 'k'])
print (db.items())#dict_items([('i', 3), ('k', 5)])
#4
b=[]
for i in range(1,4):
b.append(i**2)
print(b)#[1, 4, 9]
#5
b="chengxusheji"
print(b.split("h"))#['c', 'engxus', 'eji']
print(b.replace("e",""))#chngxushji
#6
a=[2,3,1,4,5]
print(sum(a))#15
#7
a=[2,1,4,2,2,4,5]
b=0
for i in a:
if i==2:
b=b+1
print(b)#3
程序设计题
见博客:https://blog.csdn.net/weixin_43956523/article/details/119255302