程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python測試題15道(含答案)

編輯:Python

1 以下代碼的輸出結果為:
print(eval(‘[1+2, 3]’))
A.報錯 B.‘[1+2, 3]’ C.[1+2, 3] D.[3, 3]

2 以下代碼的輸出結果為:
from fractions import Fraction
f = Fraction(2, 3)
print(f + 1)
print(f + Fraction(2, 3))
A.1.6666666666666665 4/3
B.1.6666666666666665 1.3333333333333333
C.5/3 4/3
D.5/3 1.3333333333333333

3 關於以下代碼,說法正確的是:
import decimal
print(0.1 + 0.2)
print(decimal.Decimal(0.1) + decimal.Decimal(0.2))
print(decimal.Decimal(‘0.1’) + decimal.Decimal(‘0.2’))
A.輸出3個0.3
B.第一個是不精確的0.3,後兩個是0.3
C.前兩個是不精確的0.3,最後一個是0.3
D.輸出3個不精確的0.3

4 以下代碼的輸出結果為:
a = []
a.append
print(a)
A.報錯 B.[] C.[None] D.None

5 以下代碼的輸出結果為:

glb = 123
def test():
import __main__
print(__main__.glb)
glb = 456
print(glb)
test()
print(glb)

A.報錯 B.123 456 123
C.123 456 456 D.123 123 123

6 以下代碼的輸出結果為:
sizes = [‘S’,‘M’]
colors = [‘white’,‘black’]
shirts = [(size,color) for color in colors for size in sizes]
print(shirts)
A.[(‘S’, ‘white’), (‘S’, ‘black’), (‘M’, ‘white’), (‘M’, ‘black’)]
B.[(‘S’, ‘white’), (‘M’, ‘white’), (‘S’, ‘black’), (‘M’, ‘black’)]
C.[(‘S’, ‘white’), (‘M’, ‘black’)]
D.[(‘white’, ‘S’), (‘black’, ‘M’)]、

7 以下代碼的輸出結果為:

def f(x):
if x == 0:
return 0
elif x == 1:
return 1
else:
return (x*f(x-1))
print(f(5))

A.120
B.720
C.24
D.64

8 以下代碼的輸出結果為:

def outer(fn):
print('outer')
def inner():
print('inner')
return fn
return inner
@outer
def fun():
print('fun')

A.outer B.inner C.fun D.無輸出

9 以下代碼的輸出結果為:

for i in range(3):
if i == 2:
break
print(i)
else:
print('end')

A.0 1 B.0 1 end C.0 1 2 D.0 1 2 end

10 以下代碼的輸出結果為:
lst = [[1,2], [3,4], [5,6]]
print(list(zip(*lst)))
A.報錯 B. [[1,2,3,4,5,6]
C.[[1, 3, 5], [2, 4, 6]] D.[(1, 3, 5), (2, 4, 6)]

11 以下代碼的輸出結果為:
lst=[1,2,3,4]
print(sum(x for x in lst))
A.報錯 B. 10 C.24 D.4

12 以下代碼的輸出結果為:

alist = [3, 'd' in 'abc', {
1,2}=={
2,1}]
print(any(alist))
print(all(alist))

A.True True B. True False C.False True D.False False

13 以下代碼的輸出結果為:

x = [-2,-1,0,1,2]
def test(x):
return x**2-2*x
print(max(x,key=test))

A.-2 B. 8 C.2 D.0

14 以下代碼的輸出結果為:

>>> lst = [2,3,4,5,2,3,2,2,4]
>>> max(set(lst),key=lst.count)

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

15 以下代碼的輸出結果為:

lis = ['apple','lemon','pear','peach']
def fn(x):
return x[::-1]
lis.sort(key=fn,reverse=True)
print(lis)

A.[‘apple’, ‘lemon’, ‘peach’,‘pear’]
B.[‘pear’, ‘peach’, ‘lemon’, ‘apple’]
C.[‘apple’,‘pear’, ‘lemon’, ‘peach’]
D.[‘pear’, ‘lemon’, ‘peach’, ‘apple’]

答案:DCCBB BAAAD BBADD

部分試題來自牛客網


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved