} } }

    冲刺豆瓣(13):python 口试必读

    添加时间:2013-7-27 点击量:

    原文来自:http://ilian.i-n-i.org/python-interview-question-and-answers/


    这里对这些口试题做出本身的熟悉和总结。这些题同时也是我另一篇博文冲刺豆瓣(9):Python口试题的加深版



    1.How are arguments passed – by reference of by value?


    这是关于Python对象的题目,是传值还是传引用。我感觉应当从以下几点切入:


    (1)、Python一切皆对象


    (2)、Python对象的三个特点(身份(内建函数id()可看)、类型(数据类型)、值(数据项))


    (3)、值(数据项)的可变与不成变引出了Python对象的可变与不成变、然后哪些是可变的,哪些是不成变的。


    (4)、加分项:聊聊Python的内存经管机制。冲刺豆瓣(5):Python口试题集1




    2.Do you know what list and dict comprehensions are? Can you give an example?


    这涉及到序列类型与映射类型的差别了,两者的差别是存储和接见数据的体式格式不合。序列类型应用数字类型的键(索引),映射类型应用其他对象做键,一般是字符串。还有就是次序题目。



    3.What is PEP 8?


    PEP8是浩繁pythoner们应用的编码规范,很多公司已经直接应用PEP8了。


    点击查看



    yes:
    
    Aligned with opening delimiter
    foo = long_function_name(var_one, var_two,
    var_three, var_four)

    More indentation included to distinguish this the rest.
    def long_function_name(
    var_one, var_two, var_three,
    var_four):
    print(var_one)



    No:
    
    Arguments on first line forbidden when not using vertical alignment
    foo = long_function_name(var_one, var_two,
    var_three, var_four)

    Further indentation required as indentation is not distinguishable
    def long_function_name(
    var_one, var_two, var_three,
    var_four):
    print(var_one)


    重视1:凡是应当在零丁的行



    Yes:
    
    import os
    import sys

    No:
    import sys, os


    然则可以如许: subprocess import Popen, PIPE


    重视2:哪些处所须要间距,哪些不须要



    yes:
    
    i = i + 1
    no:
    i=i+1
    ---------------
    yes:
    def complex(real, imag=0.0):
    return magic(r=real, i=imag)
    NO:
    def complex(real, imag = 0.0):
    return magic(r = real, i = imag)


    其他拜见上方链接。


    4.Do you use virtual environments?


    Yes,but google...


    推荐:
    http://pythoncentral.org/how-to-install-virtualenv-python/

    https://pypi.python.org/pypi/virtualenv


    5.Can you sum all of the elements in the list, how about to multuply them and get the result?



    >>> data = [123]
    
    >>> sum(int(i) for i in data)
    6
    >>> reduce(lambda x,y:x+int(y),data,0)
    6
    >>> num = 0
    >>> for obj in data:
    num
    +=int(obj)


    >>> num
    6



    6.Do you know what is the difference between lists and tuples? Can you give me an example for their usage?


    list 和 tuple 的不合起首是的语法,雷同点都是类似数组的容器,都可以保存随便率性类型的对象,不合是tuple中的元素不成变,list中的元素是可以改变的,是以 list比tuple多了一些经管此中保存元素的办法,如append, , pop, remove, sort, reverse等。


    值得一提的是 Python 2.6 开端有了tuple 的子类 collections.namedtuple 可以经由过程元素的名字接见tuple了 (因为它具有 attribute 语法) ,当然还可以进过元素索引下标接见。



    7.Do you know the difference between range and xrange?


    在文档中是如许写的:


    xrange([start,] stop[, step])This function is very similar to range(), but returns an ``xrange object instead of a list. This is an opaque sequence type which yields the same values as the corresponding list, without actually storing them all simultaneously. The advantage of xrange() over range() is minimal (since xrange() still has to create the values when asked for them) except when a very large range is used on a memory-starved machine or when all of the ranges elements are never used (such as when the loop is usually terminated with break).Note: xrange() is intended to be simple and fast. Implementations may impose restrictions to achieve this. The C implementation of Python restricts all arguments to native C longs (short Python integers), and also requires that the number of elements fit in a native C long.
    在Range的办法中,它会生成一个list的对象,然则在XRange中,它生成的倒是一个

    xrange的对象,当返回的器材不是很大的时辰,或者在一个轮回里,根蒂根基上都是从头查到底的景象下,这两个办法的效力差不久不多。然则,当返回的器材很大,
    或者轮回中经常会被Break出来的话,还是建议应用XRange,如许既省空间,又会进步效力。

    下面举个例子:
    若是应用range函数,履行下面的语句,将会获得后面的成果:



    >>> a = range(0,100
    >>> print type(a)#<type list>
    >>> print a

    [0,
    1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]

    >>> print a[0], a[1]
    0
    1



    然则,将开端的range用xrange调换,将会获得不合的成果:



    >>> x = xrange(0,100
    >>> type(x)
    <type xrange>
    >>> print x
    xrange(
    100
    >>> print x[0]
    0
    >>> print x[1]
    1


    这里可以很直接的看到它们的不合点,固然a[0], a[1]返回的值是雷同的。所以,今后coding的时辰还是尽可能应用xrange了


    项目组参考:http://www.linuxidc.com/Linux/2011-12/49182.htm



    8.Tell me a few differences between Python 2.x and 3.x


    参考python3.0的变更


    python 3.x 与 2.x的差别



    9.What are decorators and what is their usage?
    典范的pythonic用法


    参考:



    Pythonic到底是什么玩意儿?


    Python之道 Pythonic



     






    我所有的自负皆来自我的自卑,所有的英雄气概都来自于我的软弱。嘴里振振有词是因为心里满是怀疑,深情是因为痛恨自己无情。这世界没有一件事情是虚空而生的,站在光里,背后就会有阴影,这深夜里一片寂静,是因为你还没有听见声音。—— 马良《坦白书》
    分享到: