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,一经查实,立即删除!

相关文章

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

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

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

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

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

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

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…

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

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

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

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

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

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

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

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

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

状态机思路在程序设计中的应用

状态机思路在单片机程序设计中的应用 状态机的概念 状态机是软件编程中的一个重要概念。比这个概念更重要的是对它的灵活应用。在一个思路清晰而且高效的程序中,必然有状态机的身影浮现。 比如说一个按键命令解析程序,就可以被看做状态机:本来…

卷积核_漫画:卷积神经网络中的卷积核到底是什么?

卷积计算的直观体现如上所示,一张汽车图片经过了多次卷积操作,一开始卷积在提取低层次的特征(检测边缘),然后逐渐提取高层次的特征(完整的物体)不同的卷积探测器我们可以看到同一张图片经过不同的卷积核,得到的结果是不一样的&…

如何打开win7禁用的无线网卡服务器,Windows7如何使用批处理开启/禁用无线网卡...

对于无线网卡,Win7笔记本用户应该都不会陌生,许多笔记本用户都是在自己的电脑上安装了无线网卡之后,使用无线网络进行联网的,不过对于一些有线用户来说,无线网卡就显得并不太重要了。因此,今天小编就教大家…

超声声场模拟_超声全聚焦(TFM)简介

应读者要求,小编将介绍一下全聚焦TFM的基础知识。如有讲解不对的,欢迎批评指正。全聚焦是超声检测里面的新事物。早在2005前, Caroline Holmes、Paul D. Wilcox等国外学者就开始研究了全聚焦成像,并通过实验得出了TFM相对于常规相…

post修改服务器数据源,postgresql安装及配置超详细教程

1. 安装根据业务需求选择版本,官网下载初始化数据库执行完初始化任务之后,postgresql 会自动创建和生成两个用户和一个数据库:linux 系统用户 postgres:管理数据库的系统用户;密码由于是默认生成的,需要在系…

小程序分享到朋友圈_如何给小程序添加分享朋友圈

微信公众号更新以后,推送不是按照优先来的,你们可以把我的微信公众号点击设置为星标,以便于及时的接收信息.从微信小程序官方分享朋友圈有那么几天了,今天就分享下如何给自己的小程序添加分享朋友圈代码,几行代码的事。根据官方的提示需要基础…

JS代码大全

一、验证类 1、数字验证内 1.1 整数 /^(-|\)?\d$/.test(str) 1.2 大于0的整数 (用于传来的ID的验证) /^\d$/.test(str) 1.3 负整数的验证 /^-\d$/.test(str) 2、时间类 2.1 短时间,形如 (13:04:06) function isTime(str) …

eclipse中查看mysql_eclipse中怎样查看sqlite数据库的表

string createtable(classclazz , string tablename){//实例化一个容器,用来拼接sql语句stringbuffer sbuffer new stringbuffer();//sql语句,第一个字段为_id 主键自增,这是通用的,所以直接写死sbuffer.append("create tab…