Share some python interview questions that you might encounter

  • 2020-06-12 09:53:01
  • OfStack

This article mainly introduces several python interview questions I met recently in the interview. I will share them for your reference and study. Let's start with a detailed introduction.

1. Generate Fibonacci sequence and take the first 10 terms


def func(m):
 n,a,b = 0,1,1
 while n < m:
 yield a
 a,b = b,a+b
 n += 1
for one in func(10):
 print one

The key is to understand the role of a,b=b,a+b and yield.

2. Expands a list so that the elements in the list may also contain a list


def myextend(alist):
 tmp = []
 for one in alist:
  if isinstance(one,list):
   tmp.extend(myextend(one))
  else:
   tmp.append(one)
 return tmp
t = [1,2,5,[3,[],5,2,[57]],90]
print t
print myextend(t)

Examine the idea of recursive calls.

3. Please write the output with the following code


def test(x,l=[]):
 for o in range(x):
  l.append(o)
 print l
test(3)
test(1,[3,2,1])
test(3)

Enter as follows:


[0,1,2]
[3,2,1,0]
[0,1,2,0,1,2]

Variable types are Shared every time a function is called without passing arguments. If parameters are passed (such as type 2), they do not affect each other.

4. Given that a list contains duplicate data, keep the order of the first occurrence of elements in the list and de-weight, requiring complexity of O(n).


def fun(alist):
 result = []
 temp = set()
 for o in alist:
  if o not in temp:
   result.append(o)
   temp.add(o)
 return result

Examine the complexity of common operations such as lists, collections, and so on.

5. Given the following functions, please write the output and write the correct way:


z = [lambda x:x*i for i in range(3)]
x = [o(2) for o in z]
print x

The output is,4,4 [4]

Here we mainly investigate the closure in python, return value is a function when the knowledge point. Since the lambda function shares the i variable, i has become 2 when called, so the output is 4.

Fix it:


def func():
 def m(x):
  def n(y):
   return x * y
  return n
 return [f(one) for one in range(3)]
z = func()
x = [o(2) for o in z]
print x

Output, 4-trichlorobenzene [0]

6. Create a class and output a property. Output the value if the property exists, otherwise output the string of the property name


class Mycls(object):
 a = 0
 def __getattr__(self,name):
  print name
z = Mycls()
print z.a,z.b

The output is 0, b.

Focus on the reflection mechanism of python and the class-dependent method of SUCCes67EN__.

Note: the difference between the methods of ___ and ___, is that the former is called only if the property does not exist, returns a value, or throws an exception. The latter is called every time.

Another thing you can look at getattr() , hasattr() This built-in function.

Short-answer questions

Briefly describe the differences between py2 and py3 python's garbage collection mechanism The approach to multithreading in python, the limitations, and what other ways to do concurrency The models of epoll, select and poll are briefly described

conclusion


Related articles: