python練習題-day14


一、選擇題

1. python不支持的數據類型有:
A. char
B. int
C. float
D. list

ans:A

2.x = ‘foo’
y = 2
print(x + y)

A. foo
B. foofoo
C. foo2
D. 2
E. An exception is thrown

ans:E

3. 關於字符串下列說法錯誤的是 ()
A. 字符應該視為長度為1的字符串
B. 字符串以\0標志字符串的結束
C. 既可以用單引號,也可以用雙引號創建字符串
D. 在三引號字符串中可以包含換行回車等特殊字符

ans:B

4.以下不能創建一個字典的語句是 ()
A. dic1 = {}
B. dic2 = {123:345}
C. dic3 = {[123]:'uestc'}
D. dic4 = {(1,2,3):'uestc'}

ans:C

5.()

kvps = {'1':1,'2':2}
theCopy = kvps
kvps['1'] = 5
sum = kvps['1'] + theCopy['1']
print(sum)
A. 1
B. 2
C. 7
D. 10

ans:D

6.以下何者是不合法的布爾表達式 ()
A. x in range(6)
B. 3 = a
C. e > 5 and 4 == f
D. (x - 6) > 5

ans:B

7.下列表達式的值為True的是 ()
A. 5+4j > 2-3j
B. 3>2==2 (3>2 and 2 == 2)
C. e>5 and 4 == f
D. (x-6)>5

ans:B

8.已知x = 43,ch = 'A', y = 1,則表達式(x >= y and ch < 'b' and y)的值是 ()
A. 0
B. 1
C. 出錯
D. True

ans:B

9.下列表達式中返回為True的是 ()
A. 3 > 2 > 2
B. 'abc' > 'xyz' (‘abc’< ’xyz’)
C. 0x56 > 56
D. (3,2) > ('a','b') (元組不能比較大小)

ans:C

10.下列Python語句正確的是(多選) ()
A. min = x if x < y else y
B. max = x > y ? x : y (C語言里的,python里不對)
C. if (x>y) print(x) (沒有冒號)
D. while True :pass

ans:AD

11.若k為整型,下列while循環執行的次數為 ()

k = 1000
while k > 1:
    print k    (注意:通過這個語句說明是在python2環境下)
    k = k /2


A. 9 (python2)--(python2中 / 結果為整)
B. 10 (python3)--(python3中 / 結果保留小數)
C.11
D. 100

ans:A

12.以下敘述正確的是 ()
A. continue語句的作用是結束整個循環的執行
B. 只能在循環體內使用break語句
C. 在循環體內使用break或continue語句的作用相同
D. 從多層循環嵌套中退出是,只能用使用goto語句 (C語言是這樣)

ans:B

13.關於python中的復數,下列說法錯誤的是 ()
A. 表示復數的語法是real+image j (real + imag j)
B. 實部和虛部都是浮點數
C. 虛部必須后綴j且j不區分大小寫
D. 方法conjugate返回復數的共軛復數

ans:A

14. 下面的語句哪個會無限循環下去 ()

A. for a in range(10):
    time.sleep(10)
B. while 1<10:
    time.sleep(10)
C. while True:
    break
D. a = [3,-1,’,’]
for I in a[:]:
    if not a :
        break

ans:B

15.下面的代碼,哪些會輸出1,2,3,三個數字(多選)()

A. for i in range(3):
    Print(i)            0,1,2
B. aList = [0,1,2]
for i in aList:
    print(i+1)        1,2,3
C. i = 1
while i<3:
    print(i)            1,2
    i+=1
D. for i in range(3):
    Print(i+1)        1,2,3

ans:BD

16.python如何定義一個函數:()


A. class <name>(<type>arg1, <type>arg2,…<type>argN)
B. function <name>(arg1,arg2,…argN)
C. def <name>(arg1,arg2,…argN)
D. def <name>(<type>arg1, <type>arg2,…<type>argN)

ans:C

二、填空題

1.以下函數需要在其中引用一個全局變量k,請填寫語句:

def fun():
    global k
    k = k + 1

2. L = range(100)

(1) 取第一到第三個元素
(2) 取倒數第二個元素
(3) 取后十個元素
(4) 把L復制給L1

(1)list(L)[:3]
(2)list(L)[-2:-1]
(3)list(L)[-10:]
(4)L1=list(L)

3. 判斷dict有沒有某key用的方法:

ans:in

4.獲取list的元素個數方法:

ans len(list)

5. 以下代碼的輸出將是什么

def extendList(val,list = []):
    list.append(val)
    return list
list1 = extendList(10)
list2 = extendList(123,[])
list3 = extendList('a')
print 'list1 = %s'%list1
print 'list2 = %s'%list2
print 'list3 = %s'%list3

ans:[10,a]

[123]

[10,a]

6.下面程序的輸出結果是:

x = True
y = False
z = False
if x or y and x:
    print('yes')
else:
    print('no')

ans:yes

三、簡答題

1. 如何判斷一個變量是不是字符串

type(s)

2.is 和 == 的區別

is 比較內存地址

==比較值

3.python里面如何實現tuple和list的轉換

list(tuple)

tuple(list)

4.list和tuple有什么不同

list 不可哈希

tuple可哈希

5.如何得到列表list的交集與差集

list(set(list1)&set(list2))

list(set(list1)-set(list2))

6.python中定義函數時如何書寫可變參數和默認參數

def func(*args,默認參數)

7.*args和**kwargs在什么情況下會使用到?請給出使用**kwargs的事例

不確定參數個數

*args是接收所有按照位置傳參
**kwargs是接收所有按照關鍵字傳參
事例:裝飾器

8. 請描述unicode,utf-8,gbk等編碼之間的關系

Unicode 萬國碼

utf-8 一個英文一個字節一個中文三個字節 

gbk 一個中文兩個字節

9.如何使用python刪除一個文件

import os

OS.remove(file)

10..def func(a,b,*args,c = 0,**kwargs):
Pass
*args,**kwargs的作用是什么?

*args是接收所有按照位置傳參
**kwargs是接收所有按照關鍵字傳參

四、寫代碼

1. d = {'a':1,'b':2,'c':3}請打印出key,value對

for k in d:
    print(k,d.get(k))

2.請寫出一段python代碼實現刪除一個list里面的重復元素

#方法一
li = [1,2,3,4,1,2,5]
print(set(li)
#方法二
for i in li:
    if li.count(i)>1:
        li.remove(i)
#方法三
li1=[]
for i in li:
    if i not in li1:
        li1.append(i)
li=li1

3.使用python上機解決以下題目(13分)

(1) 斐波那契數列1,1,2,3,5,8,13,21…根據這樣的規律,編程求出400萬以內最大的斐波那契數,並求出它是第幾個斐波那契數

num1=1
num2=1
sum=0
count=2
while num1+num2<4000001:
    sum=num1+num2
    num1=num2
    num2=sum
    count+=1
print(count)

(2)dicta = {‘a’:1,’b’:2,’c’:3,’d’:4,’f’:’hello’}
dictb = {‘b’:3,’d’:5,’e’:7,’m’:9,’k’:’world’}
要求寫一段代碼,實現兩個字典的相加,不同的key對應的值保留,相同的key對應的值相加后保留,如果是字符串就拼接,如上示例得到的結果應為:

dicta = {"a":1,"b":2,"c":3,"d":4,"f":"hello"}
dictb= {"b":3,"d":5,"e":7,"m":9,"k":"world"}
for i in dictb:
    if i in dicta:
        dicta[i]=dicta[i]+dictb[i]
dictb.update(dicta)
print(dictb)
#方法二
dicta = {"a":1,"b":2,"c":3,"d":4,"f":"hello"}
dictb= {"b":3,"d":5,"e":7,"m":9,"k":"world"}
dictc={}
for i in dictb:
if i in dicta:
dictc[i]=dicta[i]+dictb[i]
else:
dictc[i]=dictb[i]
for i in dicta:
if i not in dictb:
dictc[i]=dicta[i]
print(dictc)

5. 寫一個裝飾器,可以打印輸出方法執行時長的信息

import time
def wrapper(fun):
    def inner(*args,**kwargs):
        s=time.time()
        ret=fun(*args,**kwargs)
        e=time.time()
        print(e-s)
        return ret
    return inner
@wrapper
def f(g):
    time.sleep(0.1)
    return "購買商品{}".format(g)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM