400-807-8991
奇酷学院专注于Python培训,欢迎您咨询本校Python培训班
网站首页 >Python培训 >Python就业 >Python面试笔试题目精选

Python面试笔试题目精选

来源:奇酷教育 点击量: 发表于:

对于Python面试来说,笔试是必不可少的一个环节,它能直接考验面试的设计思维和开发水平,今天奇酷学院整理了14个常见的Python笔试题目,希望在大家参加Python的过程中能有一些帮助。

Python笔试题目

1.优化以下程序

result = []
for x in range(10):
result.append(x ** 2)
print(result)

优化后

>>> result = [x**2 for x in range(10)]
>>> print result
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
>>>

2.函数、类方法定义中如何实现可选参数、可选关键词参数

def funcArgsTest(a,b,c=100,*argc,**kwarg):
sum = a + b + c
for d in argc:
sum += d
for v in kwarg.itervalues():
sum += v
return sum
print funcArgsTest(100,200,300,500,600,aa=700,bb=900,cc=1000)
==>4300

3.请解释classmethod和staticmethod的区别

class ClassTest(object):
def __init__(self,a):
self.a = a
def printk(self):
print "a is ",self.a
@classmethod
def class_method(*args):
print  "args is ",args
@classmethod
def class_method2(cls):
print "cls is ",cls
@staticmethod
def static_method(*args):
print "args is ",args
ct = ClassTest(100)
ct.printk()
ct.class_method()
ct.static_method()
ct.class_method2()
==>
a is  100
args is  (,)
args is  ()
cls is

4.请给出下列程序的运行结果

x = 0.5
while x != 1.0
print(x)
x += 0.1

此题考查浮点数比较相等的情况,x != 1.0 这样程序并不会结束

因为浮点数相等比较不能通过==,应该是一个范围,可以使用 x <= 1.0

5.请根据age分别对student_tuples及student_objects进行排序

>>>student_tuples = [
('john', 'A', 15),
('jane', 'B', 12),
('dave', 'B', 10),
]

说明:每一行的三个元素分别代表name, grade, age.

>>> class Student:
def __init__(self, name, grade, age):
self.name = name
self.grade = grade
self.age = age
def __repr__(self):
return repr((self.name, self.grade, self.age))
>>>student_objects = [
Student('john', 'A', 15),
Student('jane', 'B', 12),
Student('dave', 'B', 10),
sorted(student_tuples,key = lambda x : x[2])
sorted(student_objects,key = lambda s : s.age)
class Student:
def __init__(self, name, grade, age):
self.name = name
self.grade = grade
self.age = age
def __repr__(self):
return repr((self.name, self.grade, self.age))
student_objects = [
Student('john', 'A', 15),
Student('jane', 'B', 12),
Student('dave', 'B', 10),
]
print sorted(student_objects,key = lambda s:s.age)
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]

6.如何使用匿名函数

>>> map(lambda x : x**2,[1,2,3])
[1, 4, 9]
>>> filter(lambda x: x >1 ,[1,2,3])
[2, 3]
>>> map(lambda x : x**2,[1,2,3])
[1, 4, 9]
>>> reduce(lambda x,y: x + y,[1,2,3])
6
>>> filter(lambda x: x >1 ,[1,2,3])
[2, 3]
>>>

7.何时使用标准库 collections 中的deque?

deque其实是 double-ended queue 的缩写,翻译过来就是双端队列,它最大的好处就是实现了从队列 头部快速增加和取出对象: 

.popleft(), 
.appendleft()

但是值得注意的是,list对象的这两种用法的时间复杂度是 O(n) ,也就是说随着元素数量的增加耗时呈 线性上升。而使用deque对象则是 O(1) 的复杂度,所以当你的代码有这样的需求的时候, 一定要记得使用deque。

deque还可以限制队列长度,如果长于队列则会删除

如果是是append则会删除最左端的元素

如果是apppendleft则会删除最右端的元素

>>> dq = deque([],3)
>>> dq.append(1)
>>> dq.append(2)
>>> dq.append(3)
>>> print dq
deque([1, 2, 3], maxlen=3)
>>> dq.append(4)
>>> print dq
deque([2, 3, 4], maxlen=3)

>>> dq = deque([],3)
>>> print dq
deque([], maxlen=3)
>>> dq.append(1)
>>> dq.append(2)
>>> dq.append(3)
>>> print dq
deque([1, 2, 3], maxlen=3)
>>> dq.appendleft(4)
>>> print dq
deque([4, 1, 2], maxlen=3)
>>>

collections中有几个非常好用的数据类型

Python拥有一些内置的数据类型,比如str, int, list, tuple, dict等, collections模块在这些内置数据类型的基础上,提供了几个额外的数据类型:

namedtuple(): 生成可以使用名字来访问元素内容的tuple子类

deque: 双端队列,可以快速的从另外一侧追加和推出对象

Counter: 计数器,主要用来计数

OrderedDict: 有序字典

defaultdict: 带有默认值的字典

8.标准库 copy 中的 copy 和deepcopy的区别

>>> import copy
>>> lista = [1,2,3,['a','b']]
>>> listb = copy.copy(lista)
>>> listc = copy.deepcopy(lista)
>>> lista
[1, 2, 3, ['a', 'b']]
>>> listb
[1, 2, 3, ['a', 'b']]
>>> listc
[1, 2, 3, ['a', 'b']]
>>> lista.append(5)
>>> lista
[1, 2, 3, ['a', 'b'], 5]
>>> listb
[1, 2, 3, ['a', 'b']]
>>> listc
[1, 2, 3, ['a', 'b']]
>>> id(lista)
3085189868L
>>> id(listb)
3084763820L
>>> id(listc)
3085198380L
>>> lista[3].append('c') 
>>> lista
[1, 2, 3, ['a', 'b', 'c'], 5]
>>> listb
[1, 2, 3, ['a', 'b', 'c']]
>>> listc
[1, 2, 3, ['a', 'b']]
>>>

9.标准库 re 正则表达式 match 和 search 区别

re.match是从字符串首字母处匹配

re.search是遍历整个字符串匹配

>>> str1 = "aaa"
>>> re.match(">> m = re.match(">> m.group()
'>> str1 = "xyzaaa"
>>> m = re.match(">> m.group()
Traceback (most recent call last):
  File "", line 1, inAttributeError: 'NoneType' object has no attribute 'group'
>>> re.search(">> ms = re.search(">> ms.group()
'>> 
>>> import re
>>> help(re.match)
Help on function match in module re:
match(pattern, string, flags=0)
Try to apply the pattern at the start of the string, returning
a match object, or None if no match was found.
>>> help(re.search)
Help on function search in module re:
search(pattern, string, flags=0)
Scan through string looking for a match to the pattern, returning
a match object, or None if no match was found.

10.使用正则表达式在’’’待处理

OPTION’’’字符串中, 获取OPTION的value属性值.注意: OPTION中可能有其他的标签属性,如: 或者

>>> str1 = 'TBDOPTION'
>>> m = re.match(r"(>> m.group(3)
'"TBD"'
>>> str1 = 'TBDOPTION'
>>> m = re.match(r"(>> m.group(3)
'"TBD"'
>>> str1 = 'TBDOPTION'
>
>>> m = re.match(r"(>> m.group(3)
'"TBD"'

11.如何使用标准库pdb调试 Python 程序

在python中使用pdb模块可以进行调试

import pdb
pdb.set_trace()

也可以使用python -m pdb myscript.py这样的方式

12.什么是装饰器,如何使用装饰器

def log(level):
def dec(func):
def wrapper(*kargc,**kwargs):
print "before func was called"
func(*kargc,**kwargs)
print "after func was called"
return wrapper
return dec
@log(2)
def funcLog():
print "funcLog was called"
funcLog()
==>
before func was called
funcLog was called
after func was called

13.请解释with关键字的用法

with关键字的前提是类需要实现__enter__和__exit__

>>> with open("test.sh") as f:
...   f.read()

14.参数传递使用的是传递引用还是传递值?为什么?

对于变量(与对象相对的概念),其实,python函数参数传递可以理解为就是变量传值操作

不可变对象参数调用

def ChangeInt( a ):
a = 10
nfoo = 2 
ChangeInt(nfoo)
print nfoo #结果是2

可变对象参数调用

def ChangeList( a ):
a[0] = 10
lstFoo = [2]
ChangeList(lstFoo )
print nfoo #结果是[10]

热门文章

开课提醒

郑州

郑州Python
开课时间离开课还有

-

奇酷教育培训课程

猜你喜欢

Python前景