qzmjef 发表于 2024-8-17 03:43:41

一套真实的Python面试题,几十个题目汇总


    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1. (1)python下多线程的限制以及多进程中传递参数的方式</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">  python多线程有个全局解释器锁(global interpreter lock),这个锁的意思是任一时间只能有一个线程<span style="color: black;">运用</span>解释器,跟单cpu跑多个程序一个意思,<span style="color: black;">大众</span>都是轮着用的,这叫“并发”,不是“并行”。</p>多进程间共享数据,<span style="color: black;">能够</span><span style="color: black;">运用</span> multiprocessing.Value 和 multiprocessing.Array
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">  (2)python多线程与多进程的区别  </span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">私信<span style="color: black;">博主</span>01<span style="color: black;">就可</span>获取<span style="color: black;">海量</span>Python学习资料</p><span style="color: black;">在UNIX平台上,当某个进程终结之后,该进程<span style="color: black;">必须</span>被其父进程调用wait,否则进程<span style="color: black;">作为</span>僵尸进程(Zombie)。<span style="color: black;">因此</span>,有必要对每个Process对象调用join()<span style="color: black;">办法</span> (<span style="color: black;">实质</span>上等同于wait)。<span style="color: black;">针对</span>多线程<span style="color: black;">来讲</span>,<span style="color: black;">因为</span><span style="color: black;">仅有</span>一个进程,<span style="color: black;">因此</span>不存<span style="color: black;">这里</span>必要性。</span><span style="color: black;">多进程应该避免共享资源。在多线程中,<span style="color: black;">咱们</span><span style="color: black;">能够</span>比较容易地共享资源,<span style="color: black;">例如</span><span style="color: black;">运用</span>全局变量<span style="color: black;">或</span>传递参数。在多进程<span style="color: black;">状况</span>下,<span style="color: black;">因为</span><span style="color: black;">每一个</span>进程有自己独立的内存空间,以上<span style="color: black;">办法</span>并不合适。此时<span style="color: black;">咱们</span><span style="color: black;">能够</span><span style="color: black;">经过</span>共享内存和Manager的<span style="color: black;">办法</span>来共享资源。但<span style="color: black;">这般</span>做<span style="color: black;">加强</span>了程序的<span style="color: black;">繁杂</span>度,并<span style="color: black;">由于</span>同步的<span style="color: black;">必须</span>而降低了程序的效率。</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">2.Python是<span style="color: black;">怎样</span>进行内存管理的?</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">http://developer.51cto.com/art/201007/213585.htm</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Python引用了一个内存池(memory pool)机制,即Pymalloc机制(malloc:n.分配内存),用于管理对小块内存的申请和释放</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">内存池(memory pool)的概念:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">当 创建<span style="color: black;">海量</span>消耗小内存的对象时,频繁调用new/malloc会<span style="color: black;">引起</span><span style="color: black;">海量</span>的内存碎片,致使效率降低。内存池的概念<span style="color: black;">便是</span>预先在内存中申请<span style="color: black;">必定</span>数量的,<span style="color: black;">体积</span>相等 的内存块留作备用,当有新的内存需求时,就先从内存池中分配内存给这个需求,<span style="color: black;">不足</span>了之后再申请新的内存。<span style="color: black;">这般</span>做最<span style="color: black;">明显</span>的<span style="color: black;">优良</span><span style="color: black;">便是</span>能够减少内存碎片,<span style="color: black;">提高</span>效率。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">内存池的实现方式有<span style="color: black;">非常多</span>,性能和适用范围<span style="color: black;">亦</span>不<span style="color: black;">同样</span>。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">python中的内存管理机制——Pymalloc:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">python中的内存管理机制都有两套实现,一套是针对小对象,<span style="color: black;">便是</span><span style="color: black;">体积</span><span style="color: black;">少于</span>256bits时,pymalloc会在内存池中申请内存空间;当大于256bits,则会直接执行new/malloc的<span style="color: black;">行径</span>来申请内存空间。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">  关于释放内存方面,当一个对象的引用计数变为0时,python就会调用它的析构函数。在析构时,<span style="color: black;">亦</span>采用了内存池机制,从内存池来的内存会被归还到内存池中,以避免频繁地释放动作。</p>3.什么是lambda函数?它有什么好处?
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">http://www.kuqin.com/diveinto_python_document/apihelper_lambda.html</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">lambda 函数是一个<span style="color: black;">能够</span>接收任意多个参数(<span style="color: black;">包含</span>可选参数)并且返回单个表达式值的函数。 lambda 函数<span style="color: black;">不可</span><span style="color: black;">包括</span>命令,它们所<span style="color: black;">包括</span>的表达式<span style="color: black;">不可</span>超过一个。不要试图向lambda 函数中塞入太多的东西;<span style="color: black;">倘若</span>你<span style="color: black;">必须</span>更<span style="color: black;">繁杂</span>的东西,应该定义一个普通函数,<span style="color: black;">而后</span>想让它多长就多长。</p>4.<span style="color: black;">怎样</span>用Python输出一个Fibonacci数列?<span style="color: black;">1</span> <span style="color: black;">a,b</span> <span style="color: black;">=</span> <span style="color: black;">0</span><span style="color: black;">,</span> <span style="color: black;">1</span>
    <span style="color: black;">2</span> <span style="color: black;">while</span> <span style="color: black;">b&lt;100:</span>
    <span style="color: black;">3</span> <span style="color: black;">print</span> <span style="color: black;">(b),</span>
    <span style="color: black;">4</span> <span style="color: black;">a,</span> <span style="color: black;">b</span> <span style="color: black;">=</span> <span style="color: black;">b,</span> <span style="color: black;">a+b</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">5.介绍一下Python中webbrowser的用法?</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">webbrowser模块<span style="color: black;">供给</span>了一个高级接口来<span style="color: black;">表示</span>基于Web的文档,大部分<span style="color: black;">状况</span>下只<span style="color: black;">必须</span>简单的调用open()<span style="color: black;">办法</span>。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">webbrowser定义了如下的<span style="color: black;">反常</span>:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">exception webbrowser.Error, 当浏览器控件<span style="color: black;">出现</span>错误时会抛出这个<span style="color: black;">反常</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">webbrowser有以下方法:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">webbrowser.open(url[, new=0[, autoraise=1]])</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">这个<span style="color: black;">办法</span>是在默认的浏览器中<span style="color: black;">表示</span>url, <span style="color: black;">倘若</span>new = 0, <span style="color: black;">那样</span>url会在同一个浏览器窗口下打开,<span style="color: black;">倘若</span>new = 1, 会打开一个新的窗口,<span style="color: black;">倘若</span>new = 2, 会打开一个新的tab, <span style="color: black;">倘若</span>autoraise = true, 窗口会自动增长。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">webbrowser.open_new(url)</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在默认浏览器中打开一个新的窗口来<span style="color: black;">表示</span>url, 否则,在仅有的浏览器窗口中打开url</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">webbrowser.open_new_tab(url)</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在默认浏览器中当开一个新的tab来<span style="color: black;">表示</span>url, 否则跟open_new()<span style="color: black;">同样</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">webbrowser.get() <span style="color: black;">按照</span>name返回一个浏览器对象,<span style="color: black;">倘若</span>name为空,则返回默认的浏览器</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">webbrowser.register(name, construtor[, instance])</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">注册一个名字为name的浏览器,<span style="color: black;">倘若</span>这个浏览器类型被注册就<span style="color: black;">能够</span>用get()<span style="color: black;">办法</span>来获取。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">6.解释一下python的and-or语法</p><span style="color: black;">http://www.kuqin.com/diveinto_python_document/apihelper_andor.html</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">与C表达式 bool ? a : b类似,<span style="color: black;">然则</span>bool and a or b,当 a 为假时,不会向C表达式 bool ? a : b <span style="color: black;">同样</span>工作</p>应该将 and-or 技巧封装成一个函数:
    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">choose</span><span style="color: black;">(bool, a, b)</span>:</span>
    <span style="color: black;">return</span> (bool <span style="color: black;">and</span> <span style="color: black;">or</span> )[<span style="color: black;">0</span>]<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">由于</span> 是一个非空列表,它永远不会为假。<span style="color: black;">乃至</span> a 是 0 或 或其它假值,列表为真,<span style="color: black;">由于</span>它有一个元素。</p>7.how do I iterate over a sequence in reverse order<span style="color: black;">for</span> x in reversed(sequence):
    ... <span style="color: black;"># do something with x..</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">倘若</span>不是list, 最通用<span style="color: black;">然则</span>稍慢的<span style="color: black;">处理</span><span style="color: black;">方法</span>是:</span></p><span style="color: black;"><span style="color: black;">for</span> i <span style="color: black;">in</span> <span style="color: black;">range</span>(<span style="color: black;">len(sequence</span>)-1, -1, -1):
      x</span> = sequence
    &lt;<span style="color: black;">do</span> something with x&gt;<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">8.Python是<span style="color: black;">怎样</span>进行类型转换的?</p><span style="color: black;">http://canofy.iteye.com/blog/298263</span> <span style="color: black;">1</span> <span style="color: black;">函数 描述</span>
    <span style="color: black;">2</span> <span style="color: black;">int(x [,base ]) 将x转换为一个整数</span>
    <span style="color: black;">3</span> <span style="color: black;">long(x [,base ]) 将x转换为一个长整数</span>
    <span style="color: black;">4</span> <span style="color: black;">float(x ) 将x转换到一个浮点数</span>
    <span style="color: black;">5</span> <span style="color: black;">complex(real [,imag ]) 创建一个复数</span>
    <span style="color: black;">6</span> <span style="color: black;">str(x ) 将对象 x 转换为字符串</span>
    <span style="color: black;">7</span> <span style="color: black;">repr(x ) 将对象 x 转换为表达式字符串</span>
    <span style="color: black;">8</span> <span style="color: black;">eval(str ) 用来计算在字符串中的有效Python表达式,并返回一个对象</span>
    <span style="color: black;">9</span> <span style="color: black;">tuple(s ) 将序列 s 转换为一个元组</span>
    <span style="color: black;">10</span> <span style="color: black;">list(s ) 将序列 s 转换为一个列表</span>
    <span style="color: black;">11</span> <span style="color: black;">chr(x ) 将一个整数转换为一个字符</span>
    <span style="color: black;">12</span> <span style="color: black;">unichr(x ) 将一个整数转换为Unicode字符</span>
    <span style="color: black;">13</span> <span style="color: black;">ord(x ) 将一个字符转换为它的整数值</span>
    <span style="color: black;">14</span> <span style="color: black;">hex(x ) 将一个整数转换为一个十六进制字符串</span>
    <span style="color: black;">15</span> <span style="color: black;">oct(x ) 将一个整数转换为一个八进制字符串</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">9.Python里面<span style="color: black;">怎样</span>实现tuple和list的转换?</span></p><span style="color: black;">1</span> &gt;&gt;&gt; l = tuple(iplist)
    <span style="color: black;">2</span> &gt;&gt;&gt; <span style="color: black;">print</span> l
    <span style="color: black;">3</span> (<span style="color: black;">217.169.209.2:6666</span>, <span style="color: black;">192.227.139.106:7808</span>, <span style="color: black;">110.4.12.170:83</span>, <span style="color: black;">69.197.132.80:7808</span>, <span style="color: black;">205.164.41.101:3128</span>, <span style="color: black;">63.141.249.37:8089</span>, <span style="color: black;">27.34.142.47:9090</span>)
    <span style="color: black;">4</span> &gt;&gt;&gt; t = <span style="color: black;">list</span>(l)
    <span style="color: black;">5</span> &gt;&gt;&gt; <span style="color: black;">print</span> t
    <span style="color: black;">6</span> [<span style="color: black;">217.169.209.2:6666</span>, <span style="color: black;">192.227.139.106:7808</span>, <span style="color: black;">110.4.12.170:83</span>, <span style="color: black;">69.197.132.80:7808</span>, <span style="color: black;">205.164.41.101:3128</span>, <span style="color: black;">63.141.249.37:8089</span>, <span style="color: black;">27.34.142.47:9090</span>]<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">10.请写出一段Python代码实现删除一个list里面的重复元素</span></p><span style="color: black;">1</span> <span style="color: black;">&gt;&gt;&gt;</span> <span style="color: black;">l</span> <span style="color: black;">=</span> <span style="color: black;"></span>
    <span style="color: black;">2</span> <span style="color: black;">&gt;&gt;&gt;</span> <span style="color: black;">list(set(l))</span>
    <span style="color: black;">3</span> <span style="color: black;"></span>
    <span style="color: black;">4</span> <span style="color: black;"><span style="color: black;">或</span></span>
    <span style="color: black;">5</span> <span style="color: black;">d</span> <span style="color: black;">=</span> <span style="color: black;">{}</span>
    <span style="color: black;">6 for x in mylist:</span>
    <span style="color: black;">7</span> <span style="color: black;">d</span> <span style="color: black;">=</span> <span style="color: black;">1</span>
    <span style="color: black;">8</span> <span style="color: black;">mylist</span> <span style="color: black;">=</span> <span style="color: black;">list(d.keys())</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">11.Python<span style="color: black;">怎样</span>实现单例模式?其他23种设计模式python<span style="color: black;">怎样</span>实现?</span></p> <span style="color: black;">1</span> <span style="color: black;">#<span style="color: black;">运用</span>__metaclass__(元类)的高级python用法 </span>
    <span style="color: black;">2</span> <span style="color: black;">class</span> <span style="color: black;">Singleton2(type):</span>
    <span style="color: black;">3</span> <span style="color: black;">def</span> <span style="color: black;">__init__(cls,</span> <span style="color: black;">name,</span> <span style="color: black;">bases,</span> <span style="color: black;">dict):</span>
    <span style="color: black;">4</span> <span style="color: black;">super(Singleton2,</span> <span style="color: black;">cls).__init__(name,</span> <span style="color: black;">bases,</span> <span style="color: black;">dict)</span>
    <span style="color: black;">5</span> <span style="color: black;">cls._instance</span> <span style="color: black;">=</span> <span style="color: black;">None</span>
    <span style="color: black;">6</span> <span style="color: black;">def</span> <span style="color: black;">__call__(cls,</span> <span style="color: black;">*args,</span> <span style="color: black;">**kw):</span>
    <span style="color: black;">7 if cls._instance is None:</span>
    <span style="color: black;">8</span> <span style="color: black;">cls._instance</span> <span style="color: black;">=</span> <span style="color: black;">super(Singleton2,</span> <span style="color: black;">cls).__call__(*args,</span> <span style="color: black;">**kw)</span>
    <span style="color: black;">9</span> <span style="color: black;">return</span> <span style="color: black;">cls._instance</span>
    <span style="color: black;">10</span>
    <span style="color: black;">11</span> <span style="color: black;">class</span> <span style="color: black;">MyClass3(object):</span>
    <span style="color: black;">12</span> <span style="color: black;">__metaclass__</span> <span style="color: black;">=</span> <span style="color: black;">Singleton2</span>
    <span style="color: black;">13</span>
    <span style="color: black;">14</span> <span style="color: black;">one</span> <span style="color: black;">=</span> <span style="color: black;">MyClass3()</span>
    <span style="color: black;">15</span> <span style="color: black;">two</span> <span style="color: black;">=</span> <span style="color: black;">MyClass3()</span>
    <span style="color: black;">16</span>
    <span style="color: black;">17</span> <span style="color: black;">two.a</span> <span style="color: black;">=</span> <span style="color: black;">3</span>
    <span style="color: black;">18</span> <span style="color: black;">print</span> <span style="color: black;">one.a</span>
    <span style="color: black;">19</span> <span style="color: black;">#3 </span>
    <span style="color: black;">20</span> <span style="color: black;">print</span> <span style="color: black;">id(one)</span>
    <span style="color: black;">21</span> <span style="color: black;">#31495472 </span>
    <span style="color: black;">22</span> <span style="color: black;">print</span> <span style="color: black;">id(two)</span>
    <span style="color: black;">23</span> <span style="color: black;">#31495472 </span>
    <span style="color: black;">24</span> <span style="color: black;">print</span> <span style="color: black;">one</span> <span style="color: black;">==</span> <span style="color: black;">two</span>
    <span style="color: black;">25</span> <span style="color: black;">#True </span>
    <span style="color: black;">26</span> <span style="color: black;">print</span> <span style="color: black;">one</span> <span style="color: black;">is</span> <span style="color: black;">two</span>
    <span style="color: black;">27</span> <span style="color: black;">#True</span> <span style="color: black;">1</span> <span style="color: black;">#<span style="color: black;">运用</span>装饰器(decorator), </span>
    <span style="color: black;">2</span> <span style="color: black;">#这是一种更pythonic,更elegant的<span style="color: black;">办法</span>, </span>
    <span style="color: black;">3</span> <span style="color: black;">#单例类本身<span style="color: black;">基本</span>不<span style="color: black;">晓得</span>自己是单例的,<span style="color: black;">由于</span>他本身(自己的代码)并不是单例的 </span>
    <span style="color: black;">4</span> <span style="color: black;">def</span> <span style="color: black;">singleton(cls,</span> <span style="color: black;">*args,</span> <span style="color: black;">**kw):</span>
    <span style="color: black;">5</span> <span style="color: black;">instances</span> <span style="color: black;">=</span> <span style="color: black;">{}</span>
    <span style="color: black;">6</span> <span style="color: black;">def</span> <span style="color: black;">_singleton():</span>
    <span style="color: black;">7 if cls not in instances:</span>
    <span style="color: black;">8</span> <span style="color: black;">instances</span> <span style="color: black;">=</span> <span style="color: black;">cls(*args,</span> <span style="color: black;">**kw)</span>
    <span style="color: black;">9</span> <span style="color: black;">return</span> <span style="color: black;">instances</span>
    <span style="color: black;">10</span> <span style="color: black;">return</span> <span style="color: black;">_singleton</span>
    <span style="color: black;">11</span>
    <span style="color: black;">12</span> <span style="color: black;">@singleton</span>
    <span style="color: black;">13</span> <span style="color: black;">class</span> <span style="color: black;">MyClass4(object):</span>
    <span style="color: black;">14</span> <span style="color: black;">a</span> <span style="color: black;">=</span> <span style="color: black;">1</span>
    <span style="color: black;">15</span> <span style="color: black;">def</span> <span style="color: black;">__init__(self,</span> <span style="color: black;">x=0):</span>
    <span style="color: black;">16</span> <span style="color: black;">self.x</span> <span style="color: black;">=</span> <span style="color: black;">x</span>
    <span style="color: black;">17</span>
    <span style="color: black;">18</span> <span style="color: black;">one</span> <span style="color: black;">=</span> <span style="color: black;">MyClass4()</span>
    <span style="color: black;">19</span> <span style="color: black;">two</span> <span style="color: black;">=</span> <span style="color: black;">MyClass4()</span>
    <span style="color: black;">20</span>
    <span style="color: black;">21</span> <span style="color: black;">two.a</span> <span style="color: black;">=</span> <span style="color: black;">3</span>
    <span style="color: black;">22</span> <span style="color: black;">print</span> <span style="color: black;">one.a</span>
    <span style="color: black;">23</span> <span style="color: black;">#3 </span>
    <span style="color: black;">24</span> <span style="color: black;">print</span> <span style="color: black;">id(one)</span>
    <span style="color: black;">25</span> <span style="color: black;">#29660784 </span>
    <span style="color: black;">26</span> <span style="color: black;">print</span> <span style="color: black;">id(two)</span>
    <span style="color: black;">27</span> <span style="color: black;">#29660784 </span>
    <span style="color: black;">28</span> <span style="color: black;">print</span> <span style="color: black;">one</span> <span style="color: black;">==</span> <span style="color: black;">two</span>
    <span style="color: black;">29</span> <span style="color: black;">#True</span>
    <span style="color: black;">30</span> <span style="color: black;">print</span> <span style="color: black;">one</span> <span style="color: black;">is</span> <span style="color: black;">two</span>
    <span style="color: black;">31</span> <span style="color: black;">#True </span>
    <span style="color: black;">32</span> <span style="color: black;">one.x</span> <span style="color: black;">=</span> <span style="color: black;">1</span>
    <span style="color: black;">33</span> <span style="color: black;">print</span> <span style="color: black;">one.x</span>
    <span style="color: black;">34</span> <span style="color: black;">#1 </span>
    <span style="color: black;">35</span> <span style="color: black;">print</span> <span style="color: black;">two.x</span>
    <span style="color: black;">36</span> <span style="color: black;">#1 </span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">12.Python里面<span style="color: black;">怎样</span>拷贝一个对象?</p><span style="color: black;">http://blog.csdn.net/sharkw/article/details/1934090</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">标准库中的copy模块提供了两个<span style="color: black;">办法</span>来实现拷贝.一个<span style="color: black;">办法</span>是copy,它返回和参数<span style="color: black;">包括</span>内容<span style="color: black;">同样</span>的对象.</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">运用</span>deepcopy<span style="color: black;">办法</span>,对象中的属性<span style="color: black;">亦</span>被复制</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">13.介绍一下except的用法和<span style="color: black;">功效</span>?</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Python的except用来<span style="color: black;">捕捉</span>所有<span style="color: black;">反常</span>,<span style="color: black;">由于</span>Python里面的每次错误都会抛出一个<span style="color: black;">反常</span>,<span style="color: black;">因此</span><span style="color: black;">每一个</span>程序的错误都被当作一个运行时错误。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">14.Python中pass语句的<span style="color: black;">功效</span>是什么?</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">pass语句什么<span style="color: black;">亦</span>不做,<span style="color: black;">通常</span><span style="color: black;">做为</span>占位符<span style="color: black;">或</span>创建占位程序,pass语句不会执行任何操作</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">15.<span style="color: black;">怎样</span><span style="color: black;">晓得</span>一个python对象的类型?</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">type()</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">16.介绍一下Python下range()函数的用法?</p><span style="color: black;">http://docs.python.org/library/functions.html#range</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">range(start, stop[, step])</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">17.<span style="color: black;">怎样</span>用Python来进行<span style="color: black;">查找</span>和替换一个文本字符串?</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">能够</span><span style="color: black;">运用</span>sub()<span style="color: black;">办法</span>来进行<span style="color: black;">查找</span>和替换,sub<span style="color: black;">办法</span>的格式为:sub(replacement, string[, count=0])</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">replacement是被替换成的文本</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">string是<span style="color: black;">必须</span>被替换的文本</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">count是一个可选参数,指最大被替换的数量</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">18.Python里面search()和match()的区别?</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">match()函数只检测RE是不是在string的<span style="color: black;">起始</span>位置匹配,search()会扫描<span style="color: black;">全部</span>string<span style="color: black;">查询</span>匹配, <span style="color: black;">亦</span><span style="color: black;">便是</span>说match()<span style="color: black;">仅有</span>在0位置匹配成功的话才有返回,<span style="color: black;">倘若</span>不是<span style="color: black;">起始</span>位置匹配成功的话,match()就返回none</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">19.用Python匹配HTML tag的时候,&lt;.*&gt;和&lt;.*?&gt;有什么区别?</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">前者是贪婪匹配,会从头到尾匹配 <a style="color: black;">xyz</a>,而后者是非贪婪匹配,只匹配到<span style="color: black;">第1</span>个 &gt;。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">20.Python里面<span style="color: black;">怎样</span>生成随机数?</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">import random</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">random.random()</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">它会返回一个随机的0和1之间的浮点数</p>21.<span style="color: black;">怎样</span>用Python来发送邮件?

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">python实现发送和接收邮件功能<span style="color: black;">重点</span>用到poplib和smtplib模块。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">poplib用于接收邮件,而smtplib负责发送邮件。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">代码如下:</span></p> <span style="color: black;">1</span> <span style="color: black;">#! /usr/bin/env python</span>
    <span style="color: black;">2</span> <span style="color: black;">#coding=utf-8</span>
    <span style="color: black;">3</span> <span style="color: black;">import</span> <span style="color: black;">sys</span>
    <span style="color: black;">4</span> <span style="color: black;">import</span> <span style="color: black;">time</span>
    <span style="color: black;">5</span> <span style="color: black;">import</span> <span style="color: black;">poplib</span>
    <span style="color: black;">6</span> <span style="color: black;">import</span> <span style="color: black;">smtplib</span>
    <span style="color: black;">7</span> <span style="color: black;">#邮件发送函数</span>
    <span style="color: black;">8</span> <span style="color: black;">def</span> <span style="color: black;">send_mail():</span>
    <span style="color: black;">9 try:</span>
    <span style="color: black;">10</span> <span style="color: black;">handle</span> <span style="color: black;">=</span> <span style="color: black;">smtplib.SMTP(smtp.126.com,25)</span>
    <span style="color: black;">11</span> <span style="color: black;">handle.login(XXXX@126.com,**********)</span>
    <span style="color: black;">12</span> <span style="color: black;">msg</span> <span style="color: black;">=</span> <span style="color: black;">To: XXXX@qq.com\r\nFrom:XXXX@126.com\r\nSubject:hello\r\n</span>
    <span style="color: black;">13</span> <span style="color: black;">handle.sendmail(XXXX@126.com,XXXX@qq.com,msg)</span>
    <span style="color: black;">14</span> <span style="color: black;">handle.close()</span>
    <span style="color: black;">15</span> <span style="color: black;">return</span> <span style="color: black;">1</span>
    <span style="color: black;">16 except:</span>
    <span style="color: black;">17</span> <span style="color: black;">return</span> <span style="color: black;">0</span>
    <span style="color: black;">18</span> <span style="color: black;">#邮件接收函数</span>
    <span style="color: black;">19</span> <span style="color: black;">def</span> <span style="color: black;">accpet_mail():</span>
    <span style="color: black;">20 try:</span>
    <span style="color: black;">21</span> <span style="color: black;">p=poplib.POP3(pop.126.com)</span>
    <span style="color: black;">22</span> <span style="color: black;">p.user(pythontab@126.com)</span>
    <span style="color: black;">23</span> <span style="color: black;">p.pass_(**********)</span>
    <span style="color: black;">24</span> <span style="color: black;">ret</span> <span style="color: black;">=</span> <span style="color: black;">p.stat()</span> <span style="color: black;">#返回一个元组:(邮件数,邮件尺寸)</span>
    <span style="color: black;">25</span> <span style="color: black;">#p.retr(邮件号码)<span style="color: black;">办法</span>返回一个元组:(状态信息,邮件,邮件尺寸) </span>
    <span style="color: black;">26</span> <span style="color: black;">except</span> <span style="color: black;">poplib.error_proto,e:</span>
    <span style="color: black;">27</span> <span style="color: black;">print</span> <span style="color: black;">"Login failed:"</span><span style="color: black;">,e</span>
    <span style="color: black;">28</span> <span style="color: black;">sys.exit(1)</span>
    <span style="color: black;">29</span>
    <span style="color: black;">30</span> <span style="color: black;">#运行当前文件时,执行sendmail和accpet_mail函数</span>
    <span style="color: black;">31</span> <span style="color: black;">if</span> <span style="color: black;">__name__</span> <span style="color: black;">==</span> <span style="color: black;">"__main__":</span>
    <span style="color: black;">32</span> <span style="color: black;">send_mail()</span>
    <span style="color: black;">33</span> <span style="color: black;">accpet_mail()</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">22.Python<span style="color: black;">怎样</span>定义一个函数?</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">定义函数的格式是: def functionName(arg):</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">23.有<span style="color: black;">无</span>一个工具<span style="color: black;">能够</span><span style="color: black;">帮忙</span><span style="color: black;">查询</span>python的bug和进行静态的代码分析?</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">pycheck pylint</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">24.<span style="color: black;">怎样</span>在一个function里面设置一个全局的变量?</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">global</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">25.有两个序列a,b,<span style="color: black;">体积</span>都为n,序列元素的值任意整形数,无序;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">需求</span>:<span style="color: black;">经过</span>交换a,b中的元素,使[序列a元素的和]与[序列b元素的和]之间的差最小。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1. 分别计算a,b序列的和;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2. 求a序列和与b序列和的差值的一半,记为half;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3. 在和值大的序列中找出一个与和值小的序列中的元素max的差值最接近half的元素,即为min;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">4. 将max与min互换<span style="color: black;">就可</span>。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">26.<span style="color: black;">怎样</span>用Python删除一个文件?</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">运用</span>os.remove(filename)<span style="color: black;">或</span>os.unlink(filename);</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">27.Python<span style="color: black;">怎样</span>copy一个文件?</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">shutil模块有一个copyfile函数<span style="color: black;">能够</span>实现文件拷贝</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">28.python程序中文输出问题怎么<span style="color: black;">处理</span>?</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">用encode和decode</p>如:
    <span style="color: black;">1</span> <span style="color: black;">import os.path</span>
    <span style="color: black;">2</span> <span style="color: black;">import xlrd,sys</span>
    <span style="color: black;">3</span>
    <span style="color: black;">4</span> <span style="color: black;">Filename=’/home/tom/Desktop/1234.xls’</span>
    <span style="color: black;">5</span> <span style="color: black;">if not os.path.isfile(Filename):</span>
    <span style="color: black;">6</span> <span style="color: black;">raise NameError,”%s is not a valid filename”%Filename</span>
    <span style="color: black;">7</span>
    <span style="color: black;">8</span> <span style="color: black;">bk=xlrd.open_workbook(Filename)</span>
    <span style="color: black;">9</span> <span style="color: black;">shxrange=range(bk.nsheets)</span>
    <span style="color: black;">10</span> <span style="color: black;">print shxrange</span>
    <span style="color: black;">11</span>
    <span style="color: black;">12</span> <span style="color: black;">for x in shxrange:</span>
    <span style="color: black;">13</span> <span style="color: black;">p=bk.sheets().name.encode(‘utf-8′)</span>
    <span style="color: black;">14</span> <span style="color: black;">print p.decode(‘utf-8′)</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">办法</span>二:</p>在文件开头加上
    <span style="color: black;">1</span> <span style="color: black;">reload</span>(sys)
    <span style="color: black;">2</span> <span style="color: black;">sys</span><span style="color: black;">.setdefaultencoding</span>(‘utf8′)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">29.python代码得到列表list的交集与差集</p>交集
    <span style="color: black;">1</span> <span style="color: black;">b1=</span>
    <span style="color: black;">2</span> <span style="color: black;">b2=</span>
    <span style="color: black;">3</span> <span style="color: black;">b3 = </span>
    <span style="color: black;">4</span> <span style="color: black;">print b3</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">差集</span></p><span style="color: black;">1</span> <span style="color: black;">b1=</span>
    <span style="color: black;">2</span> <span style="color: black;">b2=</span>
    <span style="color: black;">3</span> <span style="color: black;">b3 = </span>
    <span style="color: black;">4</span> <span style="color: black;">print b3 </span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">30.写一个简单的python socket编程</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">python 编写server的<span style="color: black;">过程</span>:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1.</p>  <span style="color: black;">第1</span>步是创建socket对象。调用socket构造函数。如:
    <span style="color: black;">socket</span> = socket.socket( family, type )<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">family参数<span style="color: black;">表率</span><span style="color: black;">位置</span>家族,可为AF_INET或AF_UNIX。AF_INET家族<span style="color: black;">包含</span>Internet<span style="color: black;">位置</span>,AF_UNIX家族用于同一台<span style="color: black;">设备</span>上的进程间通信。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">type参数<span style="color: black;">表率</span>套接字类型,可为SOCK_STREAM(流套接字)和SOCK_DGRAM(数据报套接字)。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2.</p>第二步是将socket绑定到指定<span style="color: black;">位置</span>。这是<span style="color: black;">经过</span>socket对象的bind<span style="color: black;">办法</span>来实现的:<span style="color: black;">socket</span><span style="color: black;">.bind</span>( <span style="color: black;">address</span> )<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">由AF_INET所创建的套接字,address<span style="color: black;">位置</span><span style="color: black;">必要</span>是一个双元素元组,格式是(host,port)。host<span style="color: black;">表率</span>主机,port<span style="color: black;">表率</span>端口号。<span style="color: black;">倘若</span>端口号正在<span style="color: black;">运用</span>、主机名不正确或端口已被<span style="color: black;">保存</span>,bind<span style="color: black;">办法</span>将<span style="color: black;">诱发</span>socket.error<span style="color: black;">反常</span>。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3.</p>第三步是<span style="color: black;">运用</span>socket套接字的listen<span style="color: black;">办法</span>接收连接请求。<span style="color: black;">socket</span><span style="color: black;">.listen</span>( <span style="color: black;">backlog</span> )<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">backlog指定最多<span style="color: black;">准许</span>多少个客户连接到服务器。它的值<span style="color: black;">最少</span>为1。收到连接请求后,这些请求<span style="color: black;">必须</span>排队,<span style="color: black;">倘若</span>队列满,就拒绝请求。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">4.</p>  第四步是服务器套接字<span style="color: black;">经过</span>socket的accept<span style="color: black;">办法</span>等待客户请求一个连接。
    connection, address = socket.accept()<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">调用accept<span style="color: black;">办法</span>时,socket会进入“waiting”状态。客户请求连接时,<span style="color: black;">办法</span><span style="color: black;">创立</span>连接并返回服务器。accept<span style="color: black;">办法</span>返回一个含有两个元素的元组(connection,address)。<span style="color: black;">第1</span>个元素connection是新的socket对象,服务器<span style="color: black;">必要</span><span style="color: black;">经过</span>它与客户通信;第二个元素 address是客户的Internet<span style="color: black;">位置</span>。</p>5.

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">第五步是处理<span style="color: black;">周期</span>,服务器和客户端<span style="color: black;">经过</span>send和recv<span style="color: black;">办法</span>通信(传输 数据)。服务器调用send,并采用字符串形式向客户发送信息。send<span style="color: black;">办法</span>返回已发送的字符个数。服务器<span style="color: black;">运用</span>recv<span style="color: black;">办法</span>从客户接收信息。调用recv 时,服务器<span style="color: black;">必要</span>指定一个整数,它对应于可<span style="color: black;">经过</span><span style="color: black;">这次</span><span style="color: black;">办法</span>调用来接收的最大数据量。recv<span style="color: black;">办法</span>在接收数据时会进入“blocked”状态,最后返回一个字符 串,用它<span style="color: black;">暗示</span>收到的数据。<span style="color: black;">倘若</span>发送的数据量超过了recv所<span style="color: black;">准许</span>的,数据会被截短。多余的数据将缓冲于接收端。以后调用recv时,多余的数据会从缓冲区 删除(以及自上次调用recv<span style="color: black;">败兴</span>,客户可能发送的其它任何数据)。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">6. 传输结束,服务器调用socket的close<span style="color: black;">办法</span>关闭连接。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">python编写client的<span style="color: black;">过程</span>:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1. 创建一个socket以连接服务器:socket = socket.socket( family, type )</p>2.<span style="color: black;">运用</span>socket的connect<span style="color: black;">办法</span>连接服务器。<span style="color: black;">针对</span>AF_INET家族,连接格式如下:<span style="color: black;">socket</span><span style="color: black;">.connect</span>( (<span style="color: black;">host</span>,<span style="color: black;">port</span>) )<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">host<span style="color: black;">表率</span>服务器主机名或IP,port<span style="color: black;">表率</span>服务器进程所绑定的端口号。如连接成功,客户就可<span style="color: black;">经过</span>套接字与服务器通信,<span style="color: black;">倘若</span>连接失败,会<span style="color: black;">诱发</span>socket.error异常。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3. 处理<span style="color: black;">周期</span>,客户和服务器将<span style="color: black;">经过</span>send<span style="color: black;">办法</span>和recv<span style="color: black;">办法</span>通信。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">4. 传输结束,客户<span style="color: black;">经过</span>调用socket的close<span style="color: black;">办法</span>关闭连接。</p>下面给个简单的例子:

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">server.py</span></p> <span style="color: black;">1</span> <span style="color: black;">#coding:utf-8</span>
    <span style="color: black;">2</span>
    <span style="color: black;">3</span> <span style="color: black;">import</span> <span style="color: black;">socket</span>
    <span style="color: black;">4</span> <span style="color: black;">if</span> <span style="color: black;">__name__</span> <span style="color: black;">==</span> <span style="color: black;">__main__:</span>
    <span style="color: black;">5</span> <span style="color: black;">sock</span> <span style="color: black;">=</span> <span style="color: black;">socket.socket(socket.AF_INET,</span> <span style="color: black;">socket.SOCK_STREAM)</span>
    <span style="color: black;">6</span> <span style="color: black;">sock.bind((localhost,</span> <span style="color: black;">8001</span><span style="color: black;">))</span>
    <span style="color: black;">7</span> <span style="color: black;">sock.listen(5)</span>
    <span style="color: black;">8</span>
    <span style="color: black;">9 while True:</span>
    <span style="color: black;">10</span> <span style="color: black;">connection,address</span> <span style="color: black;">=</span> <span style="color: black;">sock.accept()</span>
    <span style="color: black;">11 try:</span>
    <span style="color: black;">12</span> <span style="color: black;">connection.settimeout(5)</span>
    <span style="color: black;">13</span> <span style="color: black;">buf</span> <span style="color: black;">=</span> <span style="color: black;">connection.recv(1024)</span>
    <span style="color: black;">14</span> <span style="color: black;">if</span> <span style="color: black;">buf</span> <span style="color: black;">==</span> <span style="color: black;">1:</span>
    <span style="color: black;">15</span> <span style="color: black;">connection.send(welcome</span> <span style="color: black;">to</span> <span style="color: black;">server!)</span>
    <span style="color: black;">16 else:</span>
    <span style="color: black;">17</span> <span style="color: black;">connection.send(please</span> <span style="color: black;">go</span> <span style="color: black;">out!)</span>
    <span style="color: black;">18 except socket.timeout:</span>
    <span style="color: black;">19</span> <span style="color: black;">print</span> <span style="color: black;">time out</span>
    <span style="color: black;">20</span>
    <span style="color: black;">21</span> <span style="color: black;">connection.close()</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">client.py</span></p> <span style="color: black;">1</span> <span style="color: black;">#coding:utf-8</span>
    <span style="color: black;">2</span>
    <span style="color: black;">3</span> <span style="color: black;">import</span> <span style="color: black;">socket</span>
    <span style="color: black;">4</span> <span style="color: black;">import</span> <span style="color: black;">time</span>
    <span style="color: black;">5</span>
    <span style="color: black;">6</span> <span style="color: black;">if</span> <span style="color: black;">__name__</span> <span style="color: black;">==</span> <span style="color: black;">__main__:</span>
    <span style="color: black;">7</span> <span style="color: black;">sock</span> <span style="color: black;">=</span> <span style="color: black;">socket.socket(socket.AF_INET,</span> <span style="color: black;">socket.SOCK_STREAM)</span>
    <span style="color: black;">8</span> <span style="color: black;">sock.connect((localhost,</span> <span style="color: black;">8001</span><span style="color: black;">))</span>
    <span style="color: black;">9</span> <span style="color: black;">time.sleep(2)</span>
    <span style="color: black;">10</span> <span style="color: black;">sock.send(1)</span>
    <span style="color: black;">11</span> <span style="color: black;">print</span> <span style="color: black;">sock.recv(1024)</span>
    <span style="color: black;">12</span> <span style="color: black;">sock.close()</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在终端运行server.py,<span style="color: black;">而后</span>运行clien.py,会在终端打印“welcome to server!"。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">31.python<span style="color: black;">怎样</span><span style="color: black;">捕捉</span><span style="color: black;">反常</span></p>(1)<span style="color: black;">运用</span>try和except语句来<span style="color: black;">捕捉</span><span style="color: black;">反常</span>
    <span style="color: black;">1</span> <span style="color: black;">try: </span>
    <span style="color: black;">2</span> <span style="color: black;">block </span>
    <span style="color: black;">3</span> <span style="color: black;">except ]: </span>
    <span style="color: black;">4</span> <span style="color: black;">block </span>
    <span style="color: black;">5</span>
    <span style="color: black;">6</span> <span style="color: black;">try: </span>
    <span style="color: black;">7</span> <span style="color: black;">block </span>
    <span style="color: black;">8</span> <span style="color: black;">except ]: </span>
    <span style="color: black;">9</span> <span style="color: black;">block </span>
    <span style="color: black;">10</span> <span style="color: black;">else:</span>
    <span style="color: black;">11</span> <span style="color: black;">block </span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> <span style="color: black;">捕捉</span>到的IOError错误的<span style="color: black;">仔细</span><span style="color: black;">原由</span>会被<span style="color: black;">安置</span>在对象e中,<span style="color: black;">而后</span>运行该python <span style="color: black;">反常</span>处理的except代码块<span style="color: black;">捕捉</span>所有的<span style="color: black;">反常</span></p>(2)用raise语句手工<span style="color: black;">诱发</span>一个<span style="color: black;">反常</span>:
    <span style="color: black;">1</span> <span style="color: black;">raise</span> <span style="color: black;">]</span>
    <span style="color: black;">2</span>
    <span style="color: black;">3 try:</span>
    <span style="color: black;">4</span> <span style="color: black;">raise</span> <span style="color: black;">MyError</span> <span style="color: black;">#自己抛出一个<span style="color: black;">反常</span> </span>
    <span style="color: black;">5 except MyError:</span>
    <span style="color: black;">6</span> <span style="color: black;">print</span> <span style="color: black;">a error</span>
    <span style="color: black;">7</span>
    <span style="color: black;">8</span> <span style="color: black;">raise</span> <span style="color: black;">ValueError,’invalid</span> <span style="color: black;">argument’</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">(3)采用sys模块回溯最后的<span style="color: black;">反常</span></span></p><span style="color: black;">1</span> <span style="color: black;">import sys </span>
    <span style="color: black;">2</span> <span style="color: black;">try: </span>
    <span style="color: black;">3</span> <span style="color: black;">block </span>
    <span style="color: black;">4</span> <span style="color: black;">except: </span>
    <span style="color: black;">5</span> <span style="color: black;">info=sys.exc_info() </span>
    <span style="color: black;">6</span> <span style="color: black;">print info,":",info</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">32.src = "security/afafsff/?ip=123.4.56.78&amp;id=45",请写一段代码用正则匹配出ip</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">匹配ip<span style="color: black;">位置</span>的python正则表达式</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">pattern =</p>
    <strong style="color: blue;"><span style="color: black;">^(?\d\d?|2\d|25)\.(?\d\d?|2\d|25)\.(?\d\d?|2\d|25)\.(?\d\d?|2\d|25)$</span></strong>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">故本题答案</span></p><span style="color: black;">1</span> &gt;&gt;&gt; re.findall(<span style="color: black;">r(?\d\d?|2\d|25)\.(?\d\d?|2\d|25)\.(?\d\d?|2\d|25)\.(?\d\d?|2\d|25)</span>, src)
    <span style="color: black;">2</span> [(<span style="color: black;">123</span>, <span style="color: black;">4</span>, <span style="color: black;">56</span>, <span style="color: black;">78</span>)]<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">33.写一段代码用json数据的处理方式获取{"persons":[{"name":"yu","age":"23"},{"name":"zhang","age":"34"}]}这一段json中<span style="color: black;">第1</span>个人的名字。</p> <span style="color: black;">1</span> json读取
    <span style="color: black;">2</span> &gt;&gt;&gt; <span style="color: black;">import</span> json
    <span style="color: black;">3</span> &gt;&gt;&gt; j = json.loads(<span style="color: black;">{"persons":[{"name":"yu","age":"23"},{"name":"zhang","age":"34"}]}</span>)
    <span style="color: black;">4</span> &gt;&gt;&gt; <span style="color: black;">print</span> j
    <span style="color: black;">5</span> {<span style="color: black;">upersons</span>: [{<span style="color: black;">uage</span>: <span style="color: black;">u23</span>, <span style="color: black;">uname</span>: <span style="color: black;">uyu</span>}, {<span style="color: black;">uage</span>: <span style="color: black;">u34</span>, <span style="color: black;">uname</span>: <span style="color: black;">uzhang</span>}]}
    <span style="color: black;">6</span> &gt;&gt;&gt; <span style="color: black;">print</span> j.keys()
    <span style="color: black;">7</span> [<span style="color: black;">upersons</span>]
    <span style="color: black;">8</span> &gt;&gt;&gt; <span style="color: black;">print</span> j.values()
    <span style="color: black;">9</span> [[{<span style="color: black;">uage</span>: <span style="color: black;">u23</span>, <span style="color: black;">uname</span>: <span style="color: black;">uyu</span>}, {<span style="color: black;">uage</span>: <span style="color: black;">u34</span>, <span style="color: black;">uname</span>: <span style="color: black;">uzhang</span>}]]
    <span style="color: black;">10</span> &gt;&gt;&gt; <span style="color: black;">print</span> j.values()[<span style="color: black;">0</span>]
    <span style="color: black;">11</span> [{<span style="color: black;">uage</span>: <span style="color: black;">u23</span>, <span style="color: black;">uname</span>: <span style="color: black;">uyu</span>}, {<span style="color: black;">uage</span>: <span style="color: black;">u34</span>, <span style="color: black;">uname</span>: <span style="color: black;">uzhang</span>}]
    <span style="color: black;">12</span> &gt;&gt;&gt; <span style="color: black;">print</span> j.values()[<span style="color: black;">0</span>][<span style="color: black;">0</span>]
    <span style="color: black;">13</span> {<span style="color: black;">uage</span>: <span style="color: black;">u23</span>, <span style="color: black;">uname</span>: <span style="color: black;">uyu</span>}
    <span style="color: black;">14</span> &gt;&gt;&gt; <span style="color: black;">print</span>j.values()[<span style="color: black;">0</span>][<span style="color: black;">0</span>][<span style="color: black;">name</span>]
    <span style="color: black;">15</span> yu<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">34.平衡点问题 </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">平衡点:<span style="color: black;">例如</span>int[] numbers = {1,3,5,7,8,25,4,20}; 25前面的总和为24,25后面的总和<span style="color: black;">亦</span>是24,25这个点<span style="color: black;">便是</span>平衡点;假如一个数组中的元素,其前面的部分等于后面的部分,<span style="color: black;">那样</span>这个点的位序<span style="color: black;">便是</span>平衡点 </p><span style="color: black;">需求</span>:返回任何一个平衡点
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">运用</span>sum函数累加所有的数。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">运用</span>一个变量fore来累加序列的前部。直到满足<span style="color: black;">要求</span>fore&lt;(total-number)/2;</p>python代码如下: <span style="color: black;">1</span> <span style="color: black;">numbers</span> <span style="color: black;">=</span> <span style="color: black;"></span>
    <span style="color: black;">2</span>
    <span style="color: black;">3</span> <span style="color: black;">#find total</span>
    <span style="color: black;">4</span> <span style="color: black;">total=sum(numbers)</span>
    <span style="color: black;">5</span>
    <span style="color: black;">6</span> <span style="color: black;">#find num</span>
    <span style="color: black;">7</span> <span style="color: black;">fore=0</span>
    <span style="color: black;">8 for number in numbers:</span>
    <span style="color: black;">9</span> <span style="color: black;">if</span> <span style="color: black;">fore&lt;(total-number)/2</span> <span style="color: black;">:</span>
    <span style="color: black;">10</span> <span style="color: black;">fore+=number</span>
    <span style="color: black;">11 else:</span>
    <span style="color: black;">12</span> <span style="color: black;">break</span>
    <span style="color: black;">13</span>
    <span style="color: black;">14</span> <span style="color: black;">#print answer</span>
    <span style="color: black;">15</span> <span style="color: black;">if</span> <span style="color: black;">fore</span> <span style="color: black;">==</span> <span style="color: black;">(total-number)/2</span> <span style="color: black;">:</span>
    <span style="color: black;">16</span> <span style="color: black;">print</span> <span style="color: black;">number</span>
    <span style="color: black;">17 else :</span>
    <span style="color: black;">18</span> <span style="color: black;">print</span> <span style="color: black;">rnot</span> <span style="color: black;">found</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">算法简单,<span style="color: black;">况且</span>是O(n)的,12行代码搞定。参考</p>http://blog.renren.com/share/235087438/3004327956

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">35.<span style="color: black;">安排</span>点问题: </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">安排</span>数:数组中某个元素<span style="color: black;">显现</span>的次数大于数组总数的一半时就<span style="color: black;">作为</span><span style="color: black;">安排</span>数,其所在位序<span style="color: black;">作为</span><span style="color: black;">安排</span>点;<span style="color: black;">例如</span>int[] a = {3,3,1,2,3};3为<span style="color: black;">安排</span>数,0,1,4分别为<span style="color: black;">安排</span>点;</p><span style="color: black;">需求</span>:返回任何一个<span style="color: black;">安排</span>点 <span style="color: black;">1</span> <span style="color: black;">li</span> <span style="color: black;">=</span> <span style="color: black;"></span>
    <span style="color: black;">2</span> <span style="color: black;">def</span> <span style="color: black;">main():</span>
    <span style="color: black;">3</span> <span style="color: black;">mid</span> <span style="color: black;">=</span> <span style="color: black;">len(li)/2</span>
    <span style="color: black;">4 for l in li:</span>
    <span style="color: black;">5</span> <span style="color: black;">count</span> <span style="color: black;">=</span> <span style="color: black;">0</span>
    <span style="color: black;">6</span> <span style="color: black;">i</span> <span style="color: black;">=</span> <span style="color: black;">0</span>
    <span style="color: black;">7</span> <span style="color: black;">mark</span> <span style="color: black;">=</span> <span style="color: black;">0</span>
    <span style="color: black;">8 while True:</span>
    <span style="color: black;">9</span> <span style="color: black;">if</span> <span style="color: black;">l</span> <span style="color: black;">==</span> <span style="color: black;">li:</span>
    <span style="color: black;">10</span> <span style="color: black;">count</span> <span style="color: black;">+=</span> <span style="color: black;">1</span>
    <span style="color: black;">11</span> <span style="color: black;">temp</span> <span style="color: black;">=</span> <span style="color: black;">i</span>
    <span style="color: black;">12</span> <span style="color: black;">i</span> <span style="color: black;">+=</span> <span style="color: black;">1</span>
    <span style="color: black;">13</span> <span style="color: black;">if</span> <span style="color: black;">count</span> <span style="color: black;">&gt;</span> <span style="color: black;">mid:</span>
    <span style="color: black;">14</span> <span style="color: black;">mark</span> <span style="color: black;">=</span> <span style="color: black;">temp</span>
    <span style="color: black;">15</span> <span style="color: black;">return</span> <span style="color: black;">(mark,li)</span>
    <span style="color: black;">16</span> <span style="color: black;">if</span> <span style="color: black;">i</span> <span style="color: black;">&gt;</span> <span style="color: black;">len(li)</span> <span style="color: black;">-</span> <span style="color: black;">1:</span>
    <span style="color: black;">17</span> <span style="color: black;">break</span>
    <span style="color: black;">18</span>
    <span style="color: black;">19</span> <span style="color: black;">if</span> <span style="color: black;">__name__</span> <span style="color: black;">==</span> <span style="color: black;">"__main__":</span>
    <span style="color: black;">20</span> <span style="color: black;">print</span> <span style="color: black;">main()</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 36.什么是PEP 8</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">参考:</p>http://www.python.org/dev/peps/pep-0008/
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">37.Python2.x和Python3.x的区别</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">从Python2到Python3,<span style="color: black;">非常多</span>基本的函数接口变了,<span style="color: black;">乃至</span>是,有些库或函数被去掉了,改名了。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">参考:</p>http://www.cnblogs.com/codingmylife/archive/2010/06/06/1752807.html




星☆雨 发表于 2024-8-26 17:03:50

软文发布论坛开幕式圆满成功。 http://www.fok120.com

m5k1umn 发表于 2024-10-24 08:32:46

软文发布平台 http://www.fok120.com/

qzmjef 发表于 2024-10-30 03:55:31

你的留言真是温暖如春,让我感受到了无尽的支持与鼓励。

qzmjef 发表于 2024-11-14 05:41:21

外链发布社区 http://www.fok120.com/
页: [1]
查看完整版本: 一套真实的Python面试题,几十个题目汇总