referce:python interview questions top 50

refercence:python interview questions top 15

summary

Q:

what is python?

A:

  1. Python is an interpreted language. Python does not need to be compiled before it is run. Other interpreted languages include PHP and Ruby
  2. Python is dynamically typed, this means that you don't need to state the types of variables when you declare them or anything like that.
  3. Writing Python code is quick but running it is often slower than compiled languages. Fortunately, Python allows the inclusion of C based extensions so bottlenecks can be optimised away and often are. The numpy package is a good example of this, it's really quite quick because a lot of the number crunching it does isn't actually done by Python

Q: data type

A0 = dict(zip(('a','b','c','d','e'),(1,2,3,4,5)))
A1 = range(10)
A2 = sorted([i for i in A1 if i in A0])
A3 = sorted([A0[s] for s in A0])
A4 = [i for i in A1 if i in A3]
A5 = {i:i*i for i in A1}
A6 = [[i,i*i] for i in A1]

A:

A0 = {'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4}  # the order may vary
A1 = range(0, 10) # or [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] in python 2
A2 = []
A3 = [1, 2, 3, 4, 5]
A4 = [1, 2, 3, 4, 5]
A5 = {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}
A6 = [[0, 0], [1, 1], [2, 4], [3, 9], [4, 16], [5, 25], [6, 36], [7, 49], [8, 64], [9, 81]]

Q:

Python and multi-threading

A:

Python doesn't allow multi-threading in the truest sense of the word. Python has a construct called the Global Interpreter Lock (GIL). The GIL makes sure that only one of your 'threads' can execute at any one time.

Q:

Version control

A:

Version Control helps with keeping track of who made what change to the code base; finding out when bugs were introduced to the code; keeping track of versions and releases of your software; distributing the source code amongst team members; deployment and certain automations. It allows you to roll your code back to before you broke it which is great on its own

Q:

What does this stuff mean: *args, **kwargs?

A:

Use *args when we aren't sure how many arguments are going to be passed to a function, or if we want to pass a stored list or tuple of arguments to a function. **kwargs is used when we dont know how many keyword arguments will be passed to a function, or it can be used to pass the values of a dictionary as keyword arguments. The identifiers args and kwargs are a convention.

def f(*args,**kwargs): print(args, kwargs)
l = [1,2,3]
t = (4,5,6)
d = {'a':7,'b':8,'c':9}
f(l)
f(d)
f(l,d)
f(*l,**d)

output:

([1, 2, 3],) {}
({'a': 7, 'b': 8, 'c': 9},) {}
([1, 2, 3], {'a': 7, 'b': 8, 'c': 9}) {}
(1, 2, 3) {'a': 7, 'b': 8, 'c': 9}

Q:

Decorator

A:

@my_decorator
def my_func(stuff):
do_things
equivalent to
def my_func(stuff):
do_things
my_func = my_decorator(my_func)

Q:

Python's garbage collection mechanism

A:

  • Python maintains a count of the number of references to each object in memory. If a reference count goes to zero then the associated object is no longer live and the memory allocated to that object can be freed up for something else
  • recently created objects are more likely to be dead. As objects are created, the garbage collector assigns them to generations. Each object gets one generation, and younger generations are dealt with first.

count run efficiency

import cProfile
cProfile.run("func('para')")

Q:

What is monkey patching in Python?

A:

# code at m.py
class MyClass:
def f(self):
print "f()"
import m
def monkey_f(self):
print "monkey_f()" m.MyClass.f = monkey_f
obj = m.MyClass()
obj.f()

output:

monkey_f()

conclusion: the term monkey patch only refers to dynamic modifications of a class or module at run-time.

python interview questions的更多相关文章

  1. WCF学习系列二---【WCF Interview Questions – Part 2 翻译系列】

    http://www.topwcftutorials.net/2012/09/wcf-faqs-part2.html WCF Interview Questions – Part 2 This WCF ...

  2. [译]Node.js Interview Questions and Answers (2017 Edition)

    原文 Node.js Interview Questions for 2017 什么是error-first callback? 如何避免无止境的callback? 什么是Promises? 用什么工 ...

  3. WCF学习系列三--【WCF Interview Questions – Part 3 翻译系列】

    http://www.topwcftutorials.net/2012/10/wcf-faqs-part3.html WCF Interview Questions – Part 3 This WCF ...

  4. WCF学习系列四--【WCF Interview Questions – Part 4 翻译系列】

    WCF Interview Questions – Part 4   This WCF service tutorial is part-4 in series of WCF Interview Qu ...

  5. [转]Design Pattern Interview Questions - Part 4

    Bridge Pattern, Composite Pattern, Decorator Pattern, Facade Pattern, COR Pattern, Proxy Pattern, te ...

  6. [转]Design Pattern Interview Questions - Part 2

    Interpeter , Iterator , Mediator , Memento and Observer design patterns. (I) what is Interpreter pat ...

  7. [转]Design Pattern Interview Questions - Part 3

    State, Stratergy, Visitor Adapter and fly weight design pattern from interview perspective. (I) Can ...

  8. [转]Design Pattern Interview Questions - Part 1

    Factory, Abstract factory, prototype pattern (B) What are design patterns? (A) Can you explain facto ...

  9. 101+ Manual and Automation Software Testing Interview Questions and Answers

    101+ Manual and Automation Software Testing Interview Questions and Answers http://www.softwaretesti ...

随机推荐

  1. class.forName()和.class有什么区别?

    class.forName()会初始化类的成员(静态的),最先加载的是类的静态成员变量,然后是静态代码块. 访问常量并不会导致类的初始化,但是访问静态成员会.

  2. Thirft 客户端等待时间

    thrift框架使用C++ thrift shows CLOSE_WAIL error thrift中TNonblockingServer的简单用法

  3. hdoj1789【贪心】

    题意: 已知有n个作业,每个作业呢,都是一天可以做完,每个作业都有一个截止日期,每个作业如果超过他的截止日期会扣分,最后让你求一个怎么安排求得一个最小扣的分数. 比如现在有3个作业 截止日期:3 3 ...

  4. hdoj1116【欧拉回路】

    题意: 应该是求一个路径让所有的单词能够首尾连起来,不需要头和尾的单词也连起来...应该很容易懂吧...我这里讲的好烂.. 思路: 从欧拉回路的定义引申过来. 1. 连通. 2. 入度和出度问题. 问 ...

  5. PTA PAT Judge 【模拟题,未完待续】

    The ranklist of PAT is generated from the status list, which shows the scores of the submittions. Th ...

  6. 你不知道的meta标签

    前言 meta标签可以用来做seo优化.指定移动端viewport的展现形式.设置http请求.告诉浏览器缓存静态资源的模式等等.今天整理一下使用meta标签实用的,常见的场景. meta标签的组成 ...

  7. PAT 1035 插入与归并(25)

    原题:https://pintia.cn/problem-sets/994805260223102976/problems/994805286714327040传送门: 根据维基百科的定义: 插入排序 ...

  8. nginx缓存过期

    1 原理 在默认下,请求过的内容会接受304,而从本地缓存调用.这是通过client向server发送请求,给出ETag,server确认ETag未变,则不返回内容,client调用本地缓存. 而ex ...

  9. 素数+map BestCoder Round #54 (div.2) 1002 The Factor

    题目传送门 题意:给出一个数列,问数列的乘积的一个满足条件的最小因子是什么,没有输出-1.条件是不是素数 分析:官方题解:对于每一个数字,它有用的部分其实只有它的所有质因子(包括相等的).求出所有数的 ...

  10. printf格式化输出参数

    1.类型 类型字符用以表示输出数据的类型,其格式符和意义如下表所示: 格式字符 意义 d 以十进制形式输出带符号整数(正数不输出符号) o 以八进制形式输出无符号整数(不输出前缀0) x,X 以十六进 ...