Python Interview Question and Answers

引文:http://ilian.i-n-i.org/python-interview-question-and-answers/

For the last few weeks I have been interviewing several people for Python/Django developers so I thought that it might be helpful to show the questions I am asking together with the answers. The reason is … OK, let me tell you a story first.
I remember when one of my university professors introduced to us his professor – the one who thought him. It was a really short visit but I still remember one if the things he said. “Ignorance is not bad, the bad thing is when you do no want to learn.”
So back to the reason – if you have at least taken care to prepare for the interview, look for a standard questions and their answers and learn them this is a good start. Answering these question may not get you the job you are applying for but learning them will give you some valuable knowledge about Python.
This post will include the questions that are Python specific and I’ll post the Django question separately.

  1. How are arguments passed – by reference of by value?
    The short answer is “neither”, actually it is called “call by object” or “call by sharing”(you can check here for more info). The longer one starts with the fact that this terminology is probably not the best one to describe how Python works. In Python everything is an object and all variables hold references to objects. The values of these references are to the functions. As result you can not change the value of the reference but you can modify the object if it is mutable. Remember numbers, strings and tuples are immutable, list and dicts are mutable.  May be more clear answer will be something like this (there is no short answer): Python works differently compared to other languages and there is no such a thing like passing an argument by reference or by value. If we want to compare it it will be closer to passing by reference because the object is not copied into memory instead a new name is assigned to it. I say closer and this does not mean exact because in other languages where you can pass an argument by reference, you can modify the value. In Python you also have the ability to modify the passed object but only if it is mutable type (like lists, dicts, sets, etc.). If the type of the passed object is string or int or tuple or some other kind of immutable type you can not modify it in the function. 
  2. Do you know what list and dict comprehensions are? Can you give an example?
    List/Dict comprehensions are syntax constructions to ease the creation of a list/dict based on existing iterable. According to the 3rd edition of “Learning Python” list comprehensions are generally faster than normal loops but this is something that may change between releases. Examples:
    # simple iteration
    a = []
    for x in range(10):a.append(x*2)
    # a == [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]# list comprehension
    a = [x*2 for x in range(10)]# dict comprehension
    a = {x: x*2 for x in range(10)}
    # a == {0: 0, 1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18}

     

  3. What is PEP 8?
    PEP 8 is a coding convention(a set of recommendations) how to write your Python code in order to make it more readable and useful for those after you. For more information check PEP 8.
  4. Do you use virtual environments?
    I personally and most(by my observation) of the Python developers find the virtual environment tool extremely useful. Yeah, probably you can live without it but this will make the work and support of multiple projects that requires different package versions a living hell.
  5. Can you sum all of the elements in the list, how about to multuply them and get the result?
    # the basic way
    s = 0
    for x in range(10):s += x# the right way
    s = sum(range(10))# the basic way
    s = 1
    for x in range(1, 10):s = s * x# the other way
    from operator import mul
    reduce(mul, range(1, 10))

    As for the last example, I know Guido van Rossum is not a fan of reduce, more info here, but still for some simple tasks reduce can come quite handy.

  6. Do you know what is the difference between lists and tuples? Can you give me an example for their usage?
    First list are mutable while tuples are not, and second tuples can be hashed e.g. to be used as keys for dictionaries. As an example of their usage, tuples are used when the order of the elements in the sequence matters e.g. a geographic coordinates, “list” of points in a path or route, or set of actions that should be executed in specific order. Don’t forget that you can use them a dictionary keys. For everything else use lists.
  7. Do you know the difference between range and xrange?
    Range returns a list while xrange returns a generator xrange object which takes the same memory no matter of the range size. In the first case you have all items already generated(this can take a lot of time and memory) while in the second you get the elements one by one e.g. only one element is generated and available per iteration. Simple example of generator usage can be find in the problem 2 of the “homework” for my presentation Functions in Python

    Just doing my duty by noting that xrange is NOT a generator :)

    * xrange can be indexed (generators cannot)
    * xrange has no next method! It is iterable but not an iterator.
    * Because of the previous item, xrange objects can be iterated over multiple times (generators cannot)

  8. Tell me a few differences between Python 2.x and 3.x
    There are many answers here but for me some of the major changes in Python 3.x are: all strings are now Unicode, print is now function not a statement. There is no range, it has been replaced by xrange which is removed. All classes are new style and the division of integers now returns float.
  9. What are decorators and what is their usage?
    According to Bruce Eckel’s Introduction to Python Decorators “Decorators allow you to inject or modify code in functions or classes”. In other words decorators allow you to wrap a function or class method call and execute some code before or after the execution of the original code. And also you can nest them e.g. to use more than one decorator for a specific function. Usage examples include – logging the calls to specific method, checking for permission(s), checking and/or modifying the arguments passed to the method etc.
  10. The with statement and its usage.
    In a few words the with statement allows you to executed code before and/or after a specific set of operations. For example if you open a file for reading and parsing no matter what happens during the parsing you want to be sure that at the end the file is closed. This is normally achieved using the try… finally construction but the with statement simplifies it usin the so called “context management protocol”. To use it with your own objects you just have to define __enter__ and __exit__ methods. Some standard objects like the file object automatically support this protocol. For more information you may check Understanding Python’s “with” statement.

Well I hope this will be helpful, if you have any question or suggestion feel free to comment.

Update: Due to the lots of comments on Reddit and LinkedIn, I understood that there is some misunderstanding about the post. First, the questions I have published are not the only ones I ask, the interview also includes such related to general programming knowledge and logical thinking. Second the questions above help me get a basic understanding of your Python knowledge but they are not the only think that makes my decision. Not answering some of them does not mean that you won’t get the job, but it may show me on which parts we should to work.

 

转载于:https://www.cnblogs.com/michaely/p/3352284.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/433085.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

2018年海南计算机职称考试,海南省2018年全国计算机等级考试报名时间

关于延长2018年3月全国计算机等级考试报名时间的公告2018年3月全国计算机等级考试报名时间原定为2017年12月11日-26日,为了满足广大考生报考的需要,现决定将报名时间延长至2017年12月29日17:00。请符合报考条件的考生及时上网填报报名信息和缴…

linux中 probe函数的何时调用的?

linux中 probe函数何时调用的 所以的驱动教程上都说:只有设备和驱动的名字匹配,BUS就会调用驱动的probe函数,但是有时我们要看看probe函数里面到底做了什么,还有传递给probe函数的参数我们就不知道在哪定义(反正不是我…

软件工程项目总结_复旦大学软件工程实验室来ASE实验室交流

2020年12月11日下午,复旦大学彭鑫教授一行与我院多智能体软件工程实验室开展科研工作交流。本次交流会议旨在为双方建立沟通桥梁,探讨研究问题,谋划后续合作,促使双方增进了解、加强互动、互相学习、共同进步。学院党委书记、多智…

windows无法发现任何计算机或设备,Win10系统提示windows无法与设备或资源通信如何解决...

最近有win10系统用户发现电脑无法打开网页,然后进行网络诊断的时候,提示“Windows无法与设备或资源(主DNS) 通信”,该怎么解决这样的问题呢?接下来给大家带来Win10系统提示windows无法与设备或资源通信的具体解决步骤。一、更改DN…

bbb u-boot mmc总线初始化分析

1. arch/arm/lib/ctr0.s .global _main _main: bl board_init_f 2. arch/arm/lib/spl.c void __weak board_init_f(ulong dummy) { board_init_r(NULL, 0); } 前两布应该还没有初始化串口,所以看不到打印 3. arch/arm/lib/board.c //从这个函…

scrapy 中不同页面的拼接_scrapy使用技巧总结

1. scrapy运行过程概述scrapy是一个基于python的网络爬虫框架,它读取对指定域名的网页request请求,截取对应域名的返回体,开发者可以编写解析函数,从返回体中抓取自己需要的数据,并对数据进行清洗处理或存入数据库。sc…

2021南京大学计算机复试线,南京大学2021年硕士研究生复试基本分数线

南京大学2021年硕士研究生复试基本分数线一、学术学位报考学科门类总分第1门第2门第3门第4门备注哲学[01]33555559090各院(系、所)综合考虑生源情况、本学科、专业人才培养特点及复试比例要求,根据学校复试基本分数线,可上调本院(系、所)复试的成绩要求。…

Buffers, windows, and tabs

If you’ve moved to Vim from an editor like Notepad or TextMate, you’ll be used to working with the idea of tabs in a text editor in a certain way. Specifically, a tab represents an open file; while the tab’s there, you’ve got an open file, as soon as y…

js如何获取计算机当前时间,js获取当前系统时间

搜索热词下面是编程之家 jb51.cc 通过网络收集整理的代码片段。编程之家小编现在分享给大家,也给大家做个参考。var myDate new Date();myDate.getYear(); //获取当前年份(2位)myDate.getFullYear(); //获取完整的年份(4位,1970-????)myDate.getMonth(); //获取…

docker访问宿主机mysql_docker容器内访问宿主机127.0.0.1服务

点击上方”技术生活“,选择“设为星标”做积极的人,而不是积极废人背景原因分析解决方案背景已经通过docker启动的elasticsearch 服务,监听端口9200。在宿主机中直接通过http://127.0.0.1:9200 可以直接访问,但是通过docker访问缺…

ADO.NET+Access: 3,参数 @departmentName 没有默认值

ylbtech-Error-ADO.NETAccess: 3,参数 departmentName 没有默认值。1.A,错误代码返回顶部 3,参数 departmentName 没有默认值。1.B,出错原因分析返回顶部未解决1.C,相关解决方法返回顶部作者:ylbtech出处:http://ylbtech.cnblogs.com/本文版权归作者和博…

lombok有参构造注解_Java高效开发工具: Lombok

Lombok, 一个Java开发必备效率工具,可以大大避免编写一些常用方法(get/set, hashcode等),简化开发。虽然现在IDE很多都可以通过快捷键生成POJO的一些方法了,但是如果该POJO字段发生变动后,还是需要程序员再次手动重新生成相关方法…

JavaScript操作大全整理(思维导图三--函数基础)

3.JavaScript函数基础 转载于:https://www.cnblogs.com/yuxia/p/3360806.html

__attribute__ 之weak,alias属性

Weak Alias 跟 Weak Reference 完全没有任何关系,不过是我在看到 Weak Reference 的时候想到的而已。 Weak Alias 是 gcc 扩展里的东西,实际上是函数的属性。这个东西在库的实现里面可能会经常用到,比如 glibc 里面就用了不少。抄录一段 gcc …

nginx指定配置文件启动_NGINX安全加固手册

NIGNX系统安全基线规范1.概述1.1 适用范围本配置标准的使用者包括:各事业部服务器负责人。 各事业部服务器负责人按规范要求进行认证、日志、协议、补丁升级、文件系统管理等方面的安全配置要求。对系统的安全配置审计、加固操作起到指导性作用。1.2 文档内容本文档…

口袋网咖已有服务器在使用怎么注销,口袋网咖_口袋网咖常见问题_口袋网咖专区...

口袋网咖是专门为游戏高玩打造的手机变电脑软件,虚拟电脑神器,体验各种电脑游戏,非常的方便,能让小伙伴尽情的体验手机电脑的感觉,很多小伙伴在使用过程中遇到了一些问题,快啦网为大家分享口袋网咖常见问题…

p

都不知道简历去投什么地方。游戏都卖不出去,又做不出口碑好的。这些人是心存侥幸还是心存坚持。 感觉自己搞不清楚就很难再出发。转载于:https://www.cnblogs.com/YOUEN/p/3364227.html

bbb u-boot 验证 emmc

目标: 参考"common/cmd_mmc.c"的查找emmc的代码,在mmc总线初始化函数后,遍历mmc总线,查找emmc并 打印emmc信息。 在"drivers/mmc/mmc.c"中的mmc_initialize函数的最后面添加我们的遍历函数 int mmc_initia…

统计个人已完成的工作量_团队工作量及团队价值贡献统计、核算、评审及提升的重要性...

在推行阿米巴经营模式时,需要进行企业内部产品及服务全价值分析,也就是企业内部团队产品及服务价值增值的全过程分析,团队价值增值是团队存在的目的和意义,对于团队经营来讲,团队工作量就团队的收入,团队价…

hyper服务器虚拟网卡和实际网卡,Hyper-V 3 虚拟网卡带宽应用限制

Windows Server 2012的Hyper-V 3中,打来了系列新功能,例如网卡流量限制功能。 基础架构注意的问题宿主服务器规划过程中,管理员主要考虑服务器基础架构中的CPU、内存、磁盘空间等必要因素,但是网络适配器(简称网卡)通常属于被忽略…