Python语言学习之Lang-Python
小标 2019-03-18 来源 : 阅读 1674 评论 0

摘要:本文主要向大家介绍了Python语言学习之Lang-Python,通过具体的内容向大家展示,希望对大家学习Python语言有所帮助。

本文主要向大家介绍了Python语言学习之Lang-Python,通过具体的内容向大家展示,希望对大家学习Python语言有所帮助。

Python语言学习之Lang-Python


  • Refs


  • Built-in


  • Closure


  • Collections


  • Data Structures


    • List Comprehensions



    • Comprehension


    • Sort


    • Traverse



    • Dictionary


    • List


    • Sets



  • Data Structures (Implementations)


  • Decorators


  • Exception


  • Functions


    • Parameters& Arguments



  • Generators, yield, Iterables


  • Grammar/Syntax


  • I/O


  • Iterable


  • Lambda, filter, reduce and map


  • Logic


  • Object and Class


    • metaclass


    • class variables



  • Object Types


    • 检查空对象?


    • is vs ==


    • None



  • Operators


  • Scope


  • Strings


  • Style


  • Variables, Objects, Types, References


    • 函数传参



    • python中的变量 和 引用


    • 强类型,弱类型?


    • Global vars



  • With..as..


  • Topics


    • Errors you meet



    • GIL



    • Commandline


    • Concurrency


    • Debug


    • Encoding


    • Errors


    • File


    • Garbage Collection


    • Sort


    • Regular Expression



  • System&Software


  • Questions/Problems


    • 8 Essential Python Interview Questions*




Refs



  • Learn Python the hard way



Lang Py 2.7


Built-in



  • compile


    • The compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1) built-in can be used to speed up repeated invocations of the same code with exec or eval by compiling the source into a code object beforehand.


    • Doc: Compile


    • Compile the source into a code or AST object. Code objects can be executed by an exec statement or evaluated by a call to eval().



  • eval, exec


    • What's the difference between eval, exec, and compile in Python?


    • The exec function (which was a statement in Python 2) is used for executing a dynamically created statement or program



    • only exec accepts source code that contains statements like def, for, while, import, or class


    • Both exec and eval accept 2 additional positional arguments - globals and locals - which are the global and local variable scopes that the code sees.



    • compile?




Closure



  • Late Binding Closures



Python’s closures are late binding. This means that the values of variables used in closures are looked up at the time the inner function is called. (RUNTIME)


def multipliers():
    return [lambda x : i * x for i in range(4)]

print [m(2) for m in multipliers()]


The output of the above code will be [6, 6, 6, 6] (not [0, 2, 4, 6]).


The reason for this is that Python’s closures are late binding. This means that the values of variables used in closures are looked up at the time the inner function is called. So as a result, when any of the functions returned by multipliers() are called, the value of i is looked up in the surrounding scope at that time. By then, regardless of which of the returned functions is called, the for loop has completed and i is left with its final value of 3. Therefore, every returned function multiplies the value it is passed by 3, so since a value of 2 is passed in the above code, they all return a value of 6 (i.e., 3 x 2).


对于这个例子,重要的一个点是,for i in range(4)是不属于这个lambda函数的,所以multipliers返回的是一个含有4个function的列表。late binding告诉我们,运行该lambda函数的时候,我们才计算函数的内容(也不一定要是lambda,一般的函数也可以,因为我们这里是讨论闭包),而彼时i已经变成3。这个问题的难度在于,把列表推导式使用的变量与lambda函数调用的变量用到了一起,再加上了闭包。


Collections



  • 不可不知的Python模块: collections



OrderedDict


有序的字典对象




  • Doc collections.OrderedDict


    • OrderedDict.popitem(last=True)


    • The popitem() method for ordered dictionaries returns and removes a (key, value) pair. The pairs are returned in LIFO order if last is true or FIFO order if false.





  • Ordered Dict LRUCache


  • Ordered Dict LRUCache 2



Data Structures (Types)



  • Sequential Data Types or: Sequence


  • del 用del来删除dict/list中的一个/多个元素,比使用remove要快



Dictionary



  • Doc:Dict


  • TutPoint


  • Doc:Dict 所有方法


  • Dictionary Mapping for Functions Why Doesn't Python Have Switch/Case?



  • Comprehension



    In Python 2.6 and earlier, the dict constructor can receive an iterable of key/value pairs:


    d = dict((key, value) for (key, value) in iterable)


    From Python 2.7 and 3 onwards, you can just use the dict comprehension syntax directly:


    d = {key: value for (key, value) in iterable}



    • Python: create a dictionary with list comprehension




  • Sort




    • How to Sort Python Dictionaries by Key or Value




  • Traverse




    • 注意:字典元素的顺序通常没有定义。换句话说,迭代的时候,字典中的键和值都能保证被处理,但是处理顺序不确定。如果顺序很重要的话,可以将键值保存在单独的列表中,例如迭代前进行排序。


    • items() --> a list of tuples : for k,v in d.iteritems() 也可以加上括号 for (k,v) in dict.items()


    • iteritems() - iterator-generator -> for k,v in d.iteritems() 也可以加上括号


    • 遍历方式比较


    • Python 循环遍历字典元素


    • Basics of Python Dictionary: Looping & Sorting


    • items()和iteritems()区别和使用,字典的get()函数也不错


    • 遍历python字典几种方法




List



  • Initialization 初始化


    • 会得到 ['w','u','l','a']  !!!



    • plist = ['a',1,obj1]



    • plist = ["wulala"] 得到 ['wulala']



    • plist = list() 空列表 或者 plist=[]


    • NOTE: plist = list["wula"]



  • 切片



    • range()返回列表



    • range(0,3)[::-1]  #输出[2,1,0]




    • print li[::-1]        #输出[7,6,5,4,3,2,1],省略起始索引、终止索引,步长值为-1,表示反向获取




    • python 列表切片


    • 倒序输出列表



  • the statement list = [ [ ] ] * 5 does NOT create a list containing 5 distinct lists; rather, it creates a a list of 5 references to the same list (i.e. [] here)


    • list[0].append(10) appends 10 to the first list. But since all 5 lists refer to the same list, the output is: [[10], [10], [10], [10], [10]].


    • Similarly, list[1].append(20) appends 20 to the second list. But again, since all 5 lists refer to the same list, the output is now: [[10, 20], [10, 20], [10, 20], [10, 20], [10, 20]].


    • In contrast, list.append(30) is appending an entirely new element to the “outer” list, which therefore yields the output: [[10, 20], [10, 20], [10, 20], [10, 20], [10, 20], 30].




List Comprehensions



  • Generator Comprehension, Set Comprehension, etc


  • List 推导式 深入理解


  • list comprehension中,只能使用for和if这2种语句。



    • a = 5 if b > 3 else 2 (python 中没有?:)



  • Q:Python的List Comprehension中,可以使用无限多个for、if语句,该怎么去理解这些for、if语句呢?它们之间的关系是什么呢?


  • A: Python的语法解析、字节码生成,大约分为3个阶段:

    1、将.py源代码,解析成语法树

    2、将语法树,解析成AST树

    3、根据AST树,生成字节码


    • Python在从语法树生成AST的过程中,会将List Comprehension中的for分离开来,并将每个if语句,全部归属于离他最近的左边的for语句


    • 如果变量x没有被使用过,那么变量x会成为一个局部变量,当列表推导式结束后,还可以访问变量x;否则,变量x原来的作用域是什么,现在还是什么。


    • 后续的for语句都嵌套在之前的for语句中




List Comprehension, python-course


Set Comprehension: use curly brackets instead of square brackets to create a set.

no_primes = {j for i in range(2,sqrt_n) for j in range(i*2, n, i)}


Python: List Comprehensions


S = [x**2 for x in range(10)]
V = [2**i for i in range(13)]
M = [x for x in S if x % 2 == 0] ## [0, 4, 16, 36, 64]
m = [x for x in [x**2 for x in range(10)] if x%2==0] ## [0, 4, 16, 36, 64]

>>> noprimes = [j for i in range(2, 8) for j in range(i*2, 50, i)] # 倘若写成 noprimes = [j for j in range(i*2, 50, i) for i in range(2, 8) ] 就不是本来的意思了,因为后续的for语句都嵌套在之前的for语句中
primes = [x for x in range(2, 50) if x not in noprimes]
>>> print primes
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]

[(x,y,z) for x in range(1,30) for y in range(x,30) for z in range(y,30) if x**2 + y**2 == z**2]


you can nest list comprehensions inside of each other, so you could write the above example with a single statement (without the need for the temporary variable "noprimes").


Set



  • interactive learnpython.org: sets


  • Python集合(set)类型的操作



Data Structures (Implementations)


Trees



  • Tree Traversals


  • 树和二叉树的一些基本术语


  • How to Implement a Binary Tree in Python



Decorators



  • Stackoverflow e-satis: decorators


  • A decorator is a function that expects ANOTHER function as parameter



  • Primer on Python Decorators




    • a wrapper can be seen as a wrapper of another function


    • notice the arg, and what the decorator returns (a function)




Exception




  • python 异常处理


    • 处理:except ShortInputException, xs:



    • try...except[SomeError]; try...except; raise触发异常


    • e.g. raise ShortInputException(len(s), 3)




Functions




  • python里函数定义的顺序


    • 函数里的语句在函数定义时并不执行,只有在该function being called的时候才执行



  • Zip(): 强大的zip


    • zip([seql, ...])接受一系列可迭代对象作为参数,将对象中对应的元素打包成一个个tuple(元组),然后返回由这些tuples组成的list(列表)。若传入参数的长度不等,则返回list的长度和参数中长度最短的对象相同。


    • zip()配合*号操作符,可以将已经zip过的列表对象解压




Parameters & Arguments




  • Python的函数参数传递:传值?引用?

    在python中,类型属于对象,变量是没有类型的.


    • 所有的变量都可以理解是内存中一个对象的“引用”,或者,也可以看似c中void*的感觉。所以,希望大家在看到一个python变量的时候,把变量和真正的内存对象分开。


    • “可更改”(mutable)与“不可更改”(immutable)对象:在python中,strings, tuples, 和numbers是不可更改的对象,而list,dict等则是可以修改的对象。




  • python中的传值和传引用


    • 传递参数的时候,python不允许程序员选择采用传值还是传引用。Python参数传递采用的肯定是“传对象引用”的方式。实际上,这种方式相当于传值和传引用的一种综合。


    • 如果函数收到的是一个可变对象(比如字典或者列表)的引用,就能修改对象的原始值--相当于通过“传引用”来传递对象。


    • 如果函数收到的是一个不可变对象(比如数字、字符或者元组)的引用,就不能直接修改原始对象--相当于通过“传值'来传递对象。




  • 函数参数的多种传递方法



    • * 包裹传递: 所有的参数被收集,根据位置合并成一个元组(tuple)



    • ** 包裹关键字传递:所有的参数被收集,根据位置和关键字合并成一个字典(dict)


    • 解包裹传递: 就是在传递tuple时,让tuple的每一个元素对应一个位置参数; dict的解包裹类似




  • 默认参数:


    • 如果默认参数是一个mutable,而多次调用此function时,使用了该默认参数,会怎样呢?


    • See question 1 8 Essential Python Interview Questions


    • expressions in default arguments are calculated when the function is defined, not when it’s called.




Generators, yield, Iterables



  • DOC generators


  • Generators functions allow you to declare a function that behaves like an iterator, i.e. it can be used in a for loop.


  • Python关键字yield的解释(stackoverflow)


  • To understand what yield does, you must understand what generators are. And before generators come iterables.


  • Generators are iterators, but you can only iterate over them once(第二次不会报错,但什么也不会发生). It's because they do not store all the values in memory, they generate the values on the fly



  • Yield is a keyword that is used like return, except the function will return a generator.





    • IMPORTANT: when you call the function (with yield), the code you have written in the function body does not run. The function only returns the generator object



  • extend() 是一个迭代器方法,作用于迭代器,并把参数追加到迭代器的后面



  • Python yield 用法




    • yield 简单说来就是一个生成器,生成器是这样一个函数,它记住上一次返回时在函数体中的位置。对生成器函数的第二次(或第 n 次)调用跳转至该函数中间,而上次调用的所有局部变量都保持不变。




  • GREAT TUT: IBM Python yield 使用浅析




    • 考虑可复用性,并且节省内存占用


    • 带有 yield 的函数在 Python 中被称之为 generator(生成器)




Generator Comprehension


Generator comprehensions were introduced with Python 2.6. They are simply a generator expression with a parenthesis - round brackets - around it. Otherwise, the syntax and the way of working is like list comprehension, but a generator comprehension returns a generator instead of a list


Grammar/Syntax


Late Binding


I/O




  • Python Programming/Input and Output



    • input() uses raw_input to read a string of data, and then attempts to evaluate it as if it were a Python program, and then returns the value that results.




  • File Objects


    • it can only traverse the file once.


    • You may reset the file cursor with .seek(0)



    • File objects are implemented using C’s stdio package and can be created with the built-in open() function.


    • It is an iterator


    • Actually, C's stdio is also streaming




Print


想要print始终显示在同一行,本身是在最后加上逗号即可,即:

print "xxx",


然后又想要实现,新打印的一行,冲掉之前旧的一行,达到显示出下载文件大小一点点增加,但是却始终保持同行,那么就再打印的内容最后添加上\r即可:

print "xxx\r",


Stream - io module



  • io — Core tools for working with streams¶


  • The io module provides the Python interfaces to stream handling. Under Python 2.x, this is proposed as an alternative to the built-in file object, but in Python 3.x it is the default interface to access files and streams.



Iterable




  • Python's iterator, iterable, and iteration protocols?


    • An iterable is an object that has an __iter__ method which returns an iterator, or which defines a __getitem__ method that can take sequential indexes starting from zero (and raises an IndexError when the indexes are no longer valid). So an iterable is an object that you can get an iterator from.


    • An iterator is an object with a next (Python 2) or __next__ (Python 3) method.




Whenever you use a for loop, or map, or a list comprehension, etc. in Python, the next method is called automatically to get each item from the iterator, thus going through the process of iteration.


Lambda, filter, reduce and map



  • Lambda, filter, reduce and map


  • Lambda functions are mainly used in combination with the functions filter(), map() and reduce(). The lambda feature was added to Python due to the demand from Lisp programmers.



  • Lambda 表达式有何用处?如何使用?




    • 对这个数组用第二个元素排序。可以写成 sorted(s, key=lambda x:x[1])



    • 只能由一条表达式组成


    • 一般可以在sorted, max, 这类函数里的key用lambda.比如有一个比较复杂的数组结构,s = [('a', 3), ('b', 2), ('c', 1)]




map



  • r = map(func, seq)


  • r=map(lambda x: x+"abc","gre") # r为['gabc', 'rabc', 'eabc']


  • map() can be applied to more than one list (or sequence). The lists have to have the same length and the function or lambda should be able to take more than one args.



filter



  • filter(function, list) offers an elegant way to filter out all the elements of a list, for which the function function returns True.



reduce



  • reduce(func, seq) continually applies the function func() to the sequence seq. It returns a single value.


  • it goes: [ func(func(s1, s2),s3), ... , sn ]



  • reduce(lambda x,y: x+y, [47,11,42,13]) ==>113




    • f = lambda a,b: a if (a > b) else b


    • reduce(f, [47,11,42,102,13])


    • ==> 102




Logic


if not:


if条件语句后面需要跟随bool类型的数据,即True或者False。然而,如果不是bool类型的数据,可以(自动)将其转换成bool类型的数据,转换的过程是隐式的

在Python中,None、空列表[]、空字典{}、空元组()、0等一系列代表空和无的对象会被转换成False。除此之外的其它对象都会被转化成True。

在命令if not 1中,1便会转换为bool类型的True。not是逻辑运算符非,not 1则恒为False。因此if语句if not 1之下的语句,永远不会执行。



  • there is no else if in Python, only elif



Object and Class


Metaclass



  • Stackoverflow e-satis metaclass


  • 译文: 深刻理解Python中的元类(metaclass)


  • Classes are objects too


    • This object (the class) is itself capable of creating objects (the instances), and this is why it's a class.


    • you can assign it to a variable


    • you can copy it


    • you can add attributes to it


    • you can pass it as a function parameter



  • Creating classes dynamically: like creating any object


    • e.g: in a function




  • type方法能动态的创建类: 接受一个类的描述作为参数,然后返回一个类


    • 为你的类增加方法? define a function with the proper signature and assign it as an attribute=>放入type的dictionary 参数中




Class variables



  • in Python, class variables are internally handled as dictionaries. If a variable name is not found in the dictionary of the current class, the class hierarchy (i.e., its parent classes) are searched until the referenced variable name is found (if the referenced variable name is not found in the class itself or anywhere in its hierarchy, an AttributeError occurs).


  • if any of its child classes overrides that value, then the value is changed in that child only.


  • if the value is then changed in the Parent, that change is reflected also by any children that have not yet overridden the value


  • see q3 8 interview qs



Class and its object


Copy




  • doc: copy — Shallow and deep copy operations


    • Return a deep copy of x.



    • Return a shallow copy of x.



    • import copy



    • copy.copy(x)



    • copy.deepcopy(x)




  • python中的深拷贝和浅拷贝理解


    • 利用切片操作和工厂方法list方法拷贝就叫浅拷贝,只是拷贝了最外围的对象本身,内部的元素都只是拷贝了一个引用而已。 id 一样


    • 利用copy中的deepcopy方法进行拷贝就叫做深拷贝,外围和内部元素都进行了拷贝对象本身,而不是引用。 id 不同




  • Python 拷贝对象(深拷贝deepcopy与浅拷贝copy)


    • Python中的对象之间赋值时是按引用传递的,如果需要拷贝对象,需要使用标准库中的copy模块。







    1. copy.deepcopy 深拷贝 拷贝对象及其子对象



    1. copy.copy 浅拷贝 只拷贝父对象,不会拷贝对象的内部的子对象。




Instantiation 实例化



  • woodpecker 5.4. 类的实例化



  • Python 对象的实例化过程分析 init and new


    • Python 的构造函数有两个,一个是我们最常用的 init ,另一个是很少用到的 new 。而从它们的函数定义 def new(cls, [...]) 和 def init(self, [...]) 可以知道, init 被调用时实例已经被创建(就是 self 参数所引用的);而 new 被调用的时候,实例不一定已被创建(暂时只能这么说),而 new 的第一个参数为当前类的引用。




Other



  • Copy: python中的深拷贝和浅拷贝理解


  • Swap:



    • Python为什么不需要swap(a, b)  Python以引用方式管理对象,你可以交换引用,但通常不能交换内存中的对象值。当然你也不需要这样做。




举例:


    # Definition for a binary tree node.
    # class TreeNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None

    t = root.left
    root.left = root.right
    root.right = t


相当于:


    root.left, root.right = root.right, root.left


Object Types


Assignment:




  • python赋值和拷贝----一切皆对象,参数皆引用


    • 所谓“传值”也就是赋值的意思.


    • Python不允许程序员选择采用传值还是传引用。Python参数传递采用的肯定是“传对象引用”的方式。实际上,这种方式相当于传值和传引用的一种综合。如果函数收到的是一个可变对象(比如字典或者列表)的引用,就能修改对象的原始值——相当于通过“传引用”来传递对象。如果函数收到的是一个不可变对象(比如数字、字符或者元组)的引用,就不能直接修改原始对象——相当于通过“传值”来传递对象。


    • 当人们复制列表或字典时,就复制了对象列表的引用,如果改变引用的值,则修改了原始的参数

本文由职坐标整理并发布,希望对同学们学习Python有所帮助,更多内容请关注职坐标编程语言Python频道

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程