Python初级试题25道(含答案)

1 执行下列选项的程序,会抛出异常的是:
A.

s1 = 'aabbcc'
s2 = 'abc'
count = s1.count(s2)
if count > 0 :
    print('s2是s1的子串')
else:
    print('s2不是s1的子串')

B.

s1 = 'aabbcc'
s2 = 'abc'
index = s1.index(s2)
if index > -1:
    print('s2是s1的子串')
else:
    print('s2不是s1的子串')

C.

s1 = 'aabbcc'
s2 = 'abc'
find = s1.find(s2)
if find != -1 :
    print('s2是s1的子串')
else:
    print('s2不是s1的子串')

D.

s1 = 'aabbcc'
s2 = 'abc'
if s2 in s1:
    print('s2是s1的子串')
else:
    print('s2不是s1的子串')

2 对于以下代码,描述正确的是:
list = [‘1’, ‘2’, ‘3’, ‘4’, ‘5’]
print(list[10:])
A.导致 IndexError B.输出[‘1’, ‘2’, ‘3’, ‘4’, ‘5’]
C.编译错误 D.输出[]

3 Python命令行执行下面的命令,结果为:

>>> f = (lambda x, y ,z="Win": x+y+z)
>>> f("You")

A.报错 B.‘YouWin’ C.‘You Win’ D. ‘You+Win’

4 以下代码的输出结果为:

def add(n):
    return lambda x: x+n
f = add(1)
print(f(2))

A.2 B.3 C.4 D. 5

5 以下代码的输出结果为:

L = [lambda x: x**2,lambda x: x**3,lambda x: x**4]
print(L[1](2))

A.报错 B.4 C.8 D. 16

6 以下代码的输出结果为:
str1=‘abc’
str1[2]=‘d’
print(str1)
A.报错 B.abc C.adc D. abd

7 Python命令行执行下面的命令,结果为:

>>>float(None)

A.报错 B.0 C.0.0 D. inf

8 Python命令行执行下面的命令,结果为:

>>>print(3, 'boys')

A.报错 B.3boys C.3 boys D. 3换行boys

9 以下代码的输出结果为:
a=[1,2,3,4,5]
print(a[-3:])
A.报错 B.[2, 3, 4, 5] C.[4, 5] D. [3, 4, 5]

10 以下代码的输出结果为:
strs = ’ I like python ’
one = strs.split(’ ‘)
two = strs.split()
print(one)
print(two)
A.报错 B.[’‘, ‘I’, ‘like’, ‘python’, ‘’] [‘I’, ‘like’, ‘python’]
C.[’‘, ‘I’, ‘like’, ‘python’, ‘’][’', ‘I’, ‘like’, ‘python’, ‘’] D. [‘I’, ‘like’, ‘python’] [‘I’, ‘like’, ‘python’]

11 以下代码的输出结果为:
dicts = {}
dicts[([1, 2])] = ‘abc’
print(dicts)
A.{([1,2]): ‘abc’} B.{[1,2]: ‘abc’}
C.报错 D.其他说法都不正确

12 在Python3的环境中,如下程序是实现找出1-10中奇数,则横线处应填写:

for i in range(1, 11):       
    if i % 2 == 0:           
           ______        
    print(i)

A.break B.yield C.continue D.flag

13 以下代码的输出结果为:
nl = [1,2,5,3,5]
nl.append(4)
nl.insert(0,7)
nl.sort()
print(nl)
A.[1, 2, 3, 4, 5, 5, 7] B.[0, 1, 2, 3, 4, 5, 5]
C.[1, 2, 3, 4, 5, 7] D.[7, 5, 4, 3, 2, 1]

14 以下代码的输出结果为:
lists = [1, 1, 2, 3, 4, 5, 6]
lists.remove(1)
lists.append(7)
print(lists)
A.[2,3,4,5,6] B.[1,2,3,4,5,6]
C.[2,3,4,5,6,7] D.[1,2,3,4,5,6,7]

15 Python命令行执行下面的命令,结果为:

tup = (('onion','apple'),('tomato','pear'))
for _,thing in tup:
	print(thing)

A.报错 B.apple换行pear
C.onion换行tomato D. (‘onion’,‘apple’)换行(‘tomato’,‘pear’)

16 以下代码的输出结果为:

def hhh(a,*,b,c):
    print(a,b,c)
hhh(1,2,3)

A.报错 B.1 2 3
C.1 D.2 3

17 Python命令行执行下面的命令,结果为:

>>>2+3
>>>4*_

A.报错 B. 4 C.5 D. 20

18 以下代码的输出结果为:
print(int(6.5))
print(int(‘6.5’))
A.7 7 B.7 报错 C.6 6 D. 6 报错

19 执行以下代码,下列选项中,说法正确的是()
a=b=[1,2]
a+=[3]
b=b+[3]
A.第一行时a、b的地址不同 B.a的地址发生过改变
C.最终b的值为[1, 2, 3, 3] D.最终a和b的地址相同

20 下列选项中相当于False的是:
A.{‘’} B. ({},) C. ([]) D. [[]]

21 运行下方代码段,输出的是(  )。

for i in range(10):
	for t in range(5):
		s = i + t
print(s)

A.50 B. 36 C. 15 D. 13

22 以下代码的输出结果为:

l_ids=['a','b','c']
for n,i in enumerate(l_ids,1):
	print(n,i,end=' ')

A.0 a B.0 b 1 c
C.1 b 2 c D.1 a 2 b 3 c

23 a=[1,2,3][False];print(a)的结果为:
A.报错 B.1 C.2 D.3

24 i=1;print(++i)的结果为:
A.无限循环 B.报错 C.1 D.2

25 print(float(‘12E3’))的结果为:
A.报错 B.12.3 C.12000 D.12000.0

答案:BDABC AACDB CCADB ADDCC DDBCD

  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值