Share several questions about python and several questions about python.

Source: Internet
Author: User

Share several questions about python and several questions about python.

This article mainly introduces several python interview questions recently encountered during the interview. I will share these questions for your reference. I will not talk about them here. Let's take a look at the detailed introduction:

I. Generate the Fibonacci series and take the first 10 items

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

This can be said to be a common SIMPLE algorithm question. The key point is to understand the functions of a, B = B, a + B, and yield.

2. Expand a list. The elements in the list may also contain the list.

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

Evaluate the knowledge of recursive calling.

3. Write the following code.

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

Enter the following information:

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

Variable types are used as parameters when passing parameters are not displayed. Each function call is shared. If parameters (such as the second type) are passed, they do not affect each other.

4. It is known that a list contains duplicate data, which maintains the sequence of the first appearance of elements in the list and removes duplicates. The complexity is 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

Evaluate the complexity of common operations such as list and set.

5. If the following functions are known, write out the output and write the correct statement:

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

Output: [, 4]

Here we mainly examine the knowledge points in python when the closure and return values are functions. Since lambda functions share the I variable, When I is called, I is changed to 2, so the output is 4.

Correction:

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 [0, 2, 4]

6. Create a class and output an attribute. If this attribute exists, the output value; otherwise, the string of this attribute name is output.

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

The output is 0 and B.

This section mainly describes the reflection mechanism of python and the _ getattr _ method related to classes.

Note:The difference between the _ getattr _ method and the _ getattribute _ method is that the former is called only when the property does not exist, and a value is returned or an exception is thrown. The latter is called every time.

For more information, seegetattr(),hasattr()This built-in function.

Short answer

  • Differences between py2 and py3
  • Python garbage collection mechanism
  • Multi-threaded methods in python, limitations, and other methods for concurrent processing
  • Epoll, select, and poll Models

Summary

The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.