python训练题

1.代码运行结果

v = dict.fromkeys({'k1','k2'},[])
v['k1'].append(['356'])
print(v)
v['k1'] = '777'
print(v)
{'k2': [['356']], 'k1': [['356']]}
{'k2': [['356']], 'k1': '777'}

Process finished with exit code 0

2.那一项不是内置函数 D

A:map  B: reduce C: filter D: mapreduce

3.那一项不能创建字典

dict1 = {}
dict2 = {3:5}
dict3 = {[1,2,3]:'aaa'}  #数组是可变类型,不能做建
dict4 = {(1,2,3):'aaa'}

4.那项用法不正确

df.apply(lambda  x:x.sum(),axis = 1)  #TypeError: apply() missing 1 required positional argument: 'func'
df.applymap(abs())  #TypeError: abs() takes exactly one argument (0 given)
df.rolling(10).apply(lambda x:x.rank()) #TypeError: rolling() missing 1 required positional argument: 'window'
df.applymap(np.log) #TypeError: applymap() missing 1 required positional argument: 'func'

5.非法python用法

x=y=z=1
x=(y=z+1)  #非法
x,y = y
x+=y

6.错误说法

除了字典外,所有标准对象均可用于布尔测试  #错误,字典对象也可以用于布尔测试
空字符串的布尔值是False
空列表对象的布尔值是False
值为0的任何数字对象的布尔值是False

7.下列语句是否正确

min = x if x< y else y
while True:pass
if (x>y) print(x)  #缺少分号
max = x>y ? x:y   #就是不对

8,函数如下,那个会 调用错误

def shouNumber(numbers):  传入的不是可迭代对象就会报错,字典也可以被迭代
    for n in numbers:
        print(n)

9.结果打印

def changeInt(number):
    number = number + 1
    print("number:",number)

number1 = 2
changeInt(number1)
print("number1:",number1)
number: 3
number1: 2

Process finished with exit code 0

10 下面函数都是做什么的

__new__  Create and return a new object
__iter__  用来生成迭代器。
__dict__ 储存变量
__iterator__  无此函数

11.下面的正确说法

class A:
    def showinfo(self):
        print(self.x)

a = A()
a.showinfo()
可以实例化,但是不能调用方法,编译器会有未定义提示

12 代码能否正常执行

class Hello:
    def __init__(self,name):
        self.name = name

    def showinfo(self):
        print(self.name)

h = Hello('111')
h.showinfo()

13.根据代码得出结果

g = (x*x for x in range(10))
l = [x*x for x in range(10)]

print(g==l)
print(g[0]==l[0])
print(type(g)==type(l))
print(sum(g)==sum(l))
print(g,l)
False
第二条执行失败 'generator' object is not subscriptable
False
True
 at 0x0000022469AAF1B0> [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

14 定义函数正确的方式

def func(a,*args,b):pass
def func1(a,**kwargs):pass
def func3(a,b,*args,**kwargs):pass
def func2(a,**kwargs,*args,c):pass   False

15.计算filter结果

a = [1,2,3,4,5,6,7]
b = filter(lambda  x:x > 5,a)
print(list(b))
[6, 7]

16 计算reduce结果

from functools import reduce

a = reduce(lambda x,y:x*y,range(1,4))
print(a)
6  #1*2 *3

Process finished with exit code 0

17 copy运用

import copy
a = [1,2,3,4,['a','b']]
b = copy.copy(a)
c = copy.deepcopy(a)
print(id(a[1]) == id(b[1]))
print(id(a[-1]) == id(b[-1]))
print(id(a[-1]) == id(c[-1]))

True
True
False

Process finished with exit code 0

18 正确导包

import mo
from mo import *
import mo as m

19 字典相等判断

A1 = {'小明':93,'小红':88,'小刚':100}
A2 = {'小刚':100,'小明':93,'小红':88}
print(A1 == A2)
True
Process finished with exit code 0

20 数组拼接

a = [1,2,3]
b = [4,5]
a.append(b)
print(a)
[1, 2, 3, [4, 5]]

Traceback (most recent call last):
  File "E:/DataAnalysis/project/其他/test.py", line 10, in 
    print(sum(a))
TypeError: unsupported operand type(s) for +: 'int' and 'list'

21 正则表达式

import re

a = "601988-中国银行"
print(re.findall("(\d+)-([\u4e00-\u9fa5]+)",a))
[('601988', '中国银行')]

Process finished with exit code 0

22 字典最值

a = {"a":1,"b":2,"c":3}
print(max(a))
print(max(a,key = lambda x:a[x]))
c
c
Process finished with exit code 0

24 Print False

print(0 or False and  1)

25 队列

from collections import deque

queue = deque(['Eric','John','Michael'])
queue.append('Terry')
queue.append('Graham')
print(queue)
queue.popleft()
print(queue)
deque(['Eric', 'John', 'Michael', 'Terry', 'Graham'])
deque(['John', 'Michael', 'Terry', 'Graham'])

Process finished with exit code 0

 

你可能感兴趣的:(日积跬步)