


Detailed summary of python interview questions (with answers)
This article brings you a summary of python interview questions (with answers). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. helped.
##1. What is the output of the following code? please explain.Recommended related articles: "Summary of python interview questions in 2020 (latest)"
def extendList(val, list=[]): list.append(val) return list list1 = extendList(10) list2 = extendList(123,[]) list3 = extendList('a') print "list1 = %s" % list1 print "list2 = %s" % list2 print "list3 = %s" % list3
list1 = [10, 'a'] list2 = [123] list3 = [10, 'a']
def extendList(val, list=None): if list is None: list = [] list.append(val) return list
list1 = [10] list2 = [123] list3 = ['a']
def multipliers(): return [lambda x : i * x for i in range(4)] print [m(2) for m in multipliers()]
def multipliers(): for i in range(4): yield lambda x : i * x
def multipliers(): return [lambda x, i=i : i * x for i in range(4)]
from functools import partial from operator import mul def multipliers(): return [partial(mul, i) for i in range(4)]
class Parent(object): x = 1 class Child1(Parent): pass class Child2(Parent): pass print Parent.x, Child1.x, Child2.x Child1.x = 2 print Parent.x, Child1.x, Child2.x Parent.x = 3 print Parent.x, Child1.x, Child2.x
1 1 1 1 2 1 3 2 3
def div1(x,y): print "%s/%s = %s" % (x, y, x/y) def div2(x,y): print "%s//%s = %s" % (x, y, x//y) div1(5,2) div1(5.,2) div2(5,2) div2(5.,2.)
5/2 = 2 5.0/2 = 2.5 5//2 = 2 5.0//2.0 = 2.0
例如,在两端都是整形的情况下,它不会执行整形除法
因此,在Python3中,将会是如下结果:
5/2 = 2.5 5.0/2 = 2.5 5//2 = 2 5.0//2.0 = 2.0
注: 在 Python 3 中,/ 操作符是做浮点除法,而 // 是做整除(即商没有余数,比如 10 // 3 其结果就为 3,余数会被截除掉,而 (-7) // 3 的结果却是 -3。这个算法与其它很多编程语言不一样,需要注意,它们的整除运算会向0的方向取值。而在 Python 2 中,/ 就是整除,即和 Python 3 中的 // 操作符一样)
5、下面代码的输出结果将是什么?
list = ['a', 'b', 'c', 'd', 'e'] print list[10:]
下面的代码将输出[],不会产生IndexError错误。就像所期望的那样,尝试用超出成员的个数的index来获取某个列表的成员。
例如,尝试获取list[10]和之后的成员,会导致IndexError.
然而,尝试获取列表的切片,开始的index超过了成员个数不会产生IndexError,而是仅仅返回一个空列表。
这成为特别让人恶心的疑难杂症,因为运行的时候没有错误产生,导致bug很难被追踪到。
6、考虑下列代码片段:
list = [ [ ] ] * 5 list # output? list[0].append(10) list # output? list[1].append(20) list # output? list.append(30) list # output?
2,4,6,8行将输出什么结果?试解释。
输出的结果如下:
[[], [], [], [], []] [[10], [10], [10], [10], [10]] [[10, 20], [10, 20], [10, 20], [10, 20], [10, 20]] [[10, 20], [10, 20], [10, 20], [10, 20], [10, 20], 30]
解释如下:
第一行的输出结果直觉上很容易理解,例如 list = [ [ ] ] * 5 就是简单的创造了5个空列表。然而,理解表达式list=[ [ ] ] * 5的关键一点是它不是创造一个包含五个独立列表的列表,而是它是一个创建了包含对同一个列表五次引用的列表。只有了解了这一点,我们才能更好的理解接下来的输出结果。
list[0].append(10) 将10附加在第一个列表上。
但由于所有5个列表是引用的同一个列表,所以这个结果将是:
[[10], [10], [10], [10], [10]]
同理,list[1].append(20)将20附加在第二个列表上。但同样由于5个列表是引用的同一个列表,所以输出结果现在是:
[[10, 20], [10, 20], [10, 20], [10, 20], [10, 20]]
作为对比, list.append(30)是将整个新的元素附加在外列表上,因此产生的结果是: [[10, 20], [10, 20], [10, 20], [10, 20], [10, 20], 30].
7、Given a list of N numbers。
给定一个含有N个数字的列表。
使用单一的列表生成式来产生一个新的列表,该列表只包含满足以下条件的值:
(a)偶数值
(b)元素为原始列表中偶数切片。
例如,如果list[2]包含的值是偶数。那么这个值应该被包含在新的列表当中。因为这个数字同时在原始列表的偶数序列(2为偶数)上。然而,如果list[3]包含一个偶数,
那个数字不应该被包含在新的列表当中,因为它在原始列表的奇数序列上。
对此问题的简单解决方法如下:
[x for x in list[::2] if x%2 == 0]
例如,给定列表如下:
list = [ 1 , 3 , 5 , 8 , 10 , 13 , 18 , 36 , 78 ]
列表生成式[x for x in list[::2] if x%2 == 0] 的结果是,
[10, 18, 78]
这个表达式工作的步骤是,第一步取出偶数切片的数字,
第二步剔除其中所有奇数。
8、给定以下字典的子类,下面的代码能够运行么?为什么?
class DefaultDict(dict): def __missing__(self, key): return []d = DefaultDict() d['florp'] = 127
能够运行。
当key缺失时,执行DefaultDict类,字典的实例将自动实例化这个数列。
相关学习推荐:python视频教程
The above is the detailed content of Detailed summary of python interview questions (with answers). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

Using python in Linux terminal...

Fastapi ...
