zoukankan      html  css  js  c++  java
  • Python Note

    @1:Python的语言特性: 

      Python是一门具有强类型(即变量类型是强制要求的)、动态性隐式类型(不需要做变量声明)、大小写敏感(var和VAR代表了

    不同的变量)以及面向对象(一切皆为对象)等特点的编程语言。 

    @2: 在Python中,strings, tuples, 和numbers是不可更改的对象,而list, dict等则是可以修改的对象。(NOTE: tuple不可变)

    >>> aTuple = (1, 3)
    >>> aTuple
    (1, 3)
    >>> id(aTuple)
    166931436
    >>> aTuple[0] = 2
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: 'tuple' object does not support item assignment
    
    >>> string = "hello" 
    >>> string
    'hello'
    >>> string[0] = "a"
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: 'str' object does not support item assignment
    >>>

    @3: list, tuple, str都是有序的;dict是无序的。

    因为list, tuple, str有序,所以均可以通过下标进行索引,而dict是无序的,所以不能通过下标进行索引,只能通过键进行索引。

    @4:Python set:

      python的set和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素. 集合对象还支持union(联合),

    intersection(交),difference(差)和sysmmetric difference(对称差集)等数学运算.

      sets 支持 x in set, len(set),和 for x in set。作为一个无序的集合,sets不记录元素位置或者插入点。因此,sets不支持

    indexing, slicing, 或其它类序列(sequence-like)的操作

    @5: 常用Python查询方式: help(abs), dir(abs), abs.__doc__

    dir()会显示该对象的所有的方法.

    @6:https://docs.python.org/2/library/collections.html

    @7: private - protected - public in Python:

    All class members (including the data members) are public and all the methods are virtual in Python.

    One exception: If you use data members with names using the double underscore prefix such as __privatevar, Python

    uses name-mangling to effectively make it a private variable.

    Thus, the convention followed is that any variable that is to be used only within the class or object should begin with an

    underscore and all other names are public and can be used by other classes/objects. Remember that this is only a

    convention and is not enforced by Python (except for the double underscore prefix).

    Also, note that the __del__ method is analogous to the concept of a destructor. 

    类的成员函数默认都相当于是public的,但是默认开头为__的为私有变量,虽然是私有,但是我们还可以通过一定的手段访问到,

    即Python不存在真正的私有变量。如:

    __priValue = 0    # 会自动变形为"_类名__priValue"的成员变量

    @8: Difference between '/' and '//' in Python:

    /   除 - x除以y
    // 取整除 - 返回商的整数部分

    >>> 9/2
    4
    >>> 9//2
    4
    >>> 9.0/2
    4.5
    >>> 9.0//2
    4.0
    >>>

    @9: Python中的and, or 是短路操作。 

    @10: 一般说来,应该避免使用from..import 而使用import 语句,因为这样可以使你的程序更加易读,也可以避免名称的冲突。

    @11: python2 和python3 的print和input是不一样的

    python3中的input相当于Python2中的raw_input

    python3:

    print("hello", end="")  #无换行
    print "hello",       #无换行(最后的","不能少)

     python3中字典类型d.has_key()函数已经不用了. 只在python2中使用.

     @12:

     class A:                                                                          
         def __init__(self, name):
             print "Enter A __init__()"
             self.name = name
             print "Exit A __init__()"
         def show(self):
             print self.name
     
     class B(A):
         def __init__(self, name, sex):
             #Python必须显式调用父类的构造函数[__init__()],而且必须有参数self
             print "Enter B __init__()"
             A.__init__(self, name)
             self.sex = sex 
             print "Exit B __init__()"
         def show(self):
             print self.name, self.sex

     @13:绑定方法 & 未绑定方法

    _metaclass__ = type                                                   
     class A:
         def modify(self, happy):
             self.happy = happy
     
         def show(self):
             print(self.happy)
     
     def main():
         a = A() 
         #a.show()    #AttributeError: 'A' object has no attribute 'happy'
         #A.show(a)   #AttributeError: 'A' object has no attribute 'happy'
         a.happy = False
         a.show()    #False
         A.show(a)   #False
         a.modify(True)  
         a.show()    #True
         A.show(a)   #True
         A.modify(a, False)
         a.show()    #False
         A.show(a)   #False
     
     if __name__ == '__main__':
         main()

      在调用一个实例的方法时,该方法的self参数会自动绑定到该实例上(这称为绑定方法)。

    但如果直接调用类的方法(例如Base.__init__),那么就没有实例会被绑定,这样就可以自由地提供

    需要的self参数,这样的方法称为未绑定方法。 

    @14: Python 中显示数值的二进制补码形式:

    from struct import * 
    >>> pack('b', -2) 
    'xfe' 

     @15:

    encode('gb2312') #以gb2312编码对unicode对象进行编码
    decode('gb2312') #以gb2312编码对字符串进行解码,以获取unicode

     @16: IPython Shortcuts

    1. Tab & ?
    2. Ctrl-L 清屏
    3. `ipython --TerminalInteractiveShell.editing_mode=vi`
    4. !pwd 在系统中执行pwd命令

    Reference:

    快速入门: http://blog.jobbole.com/43922/

  • 相关阅读:
    ThinkPHP where方法:设置查询或操作条件
    微信小程序showToast延迟跳转页面
    uniapp微信小程序授权登录
    什么是二维码,什么是QR码?
    微信小程序授权登录开发流程图
    微信小程序提交审核并发布详细流程(一)
    微信小程序提交审核并发布详细流程2
    uniapp医院预约挂号微信小程序
    《爆款文案》写文案只需要四个步骤
    Spark学习笔记——读写ScyllaDB
  • 原文地址:https://www.cnblogs.com/lxw0109/p/python_note.html
Copyright © 2011-2022 走看看