zoukankan      html  css  js  c++  java
  • python 知识点总结

       好久没有写过博客了,是因为工作需要正在学习Python,我总结了一下部分知识点,拿出来给大家分享一下:

    1,标识符及保留字
    and             elif                    global                  or
    assert          else                    if                      pass
    break           except                  import                  print
    class           exec                    in                      raise
    continue        finally                 is                      return
    def             for                     lambda                  try
    del             from                    not                     while
    2,标准特殊字符
    字符   描述
         续行符
    \\    反斜杠
    \'    单引号
    \"    双引号
    \a    Bell(音箱发出吡的一声)
    \b    退格符
    \e    Escape
    \0    Null(空值)
    \n    换行符,等价于\x0a和\cJ
    \v    垂直制表符,等价于\x0b和\cK
    \t    水平制表符,等价于\x09和\cI
    \r    回车符,等价于\x0d和\cM
    \f    换页符,等价于\x0c和\cL
    \OOO  八进制值(000-377)
    \xhh  十六进制值(x00-xff)
    \un   Unicode字符值,n是四个十六进制数字表示的Unicode字符
    3,运算符、分隔符及特殊符号
    Python 目前支持以下运算符:

                    **      //                              ^
    +=      -=      *=      **=     //=     /=      %=      >=     &=      |=      ^=
                     ==      !=     

    下边的这些可以作为表达式,列表,字典,以及语句不同部分的分隔符号:

                                           ;
    4,内建类型
    分类                            类型名称                        描述
    None                            NoneType                        null 对象
    数值                            IntType                         整数
                                    LongType                        任意精度整数
                                    FloatType                       浮点数
                                    ComplexType                     复数
    序列                            StringType                      字符串
                                    UnicodeType                     Unicode字符串
                                    ListType                        列表
                                    TupleType                       元组
                                    XRangeType                      xrange()函数返回的对象
                                    BufferType                      buffer()函数返回的对象
    映射                            DictType                        字典
    可调用类型                      BuiltinFunctionType             内建函数
                                    BuiltinMethodType               内建方法
                                    ClassType                      
                                    FunctionType                    用户定义函数
                                    InstanceType                    类实例
                                    MethodType                      Bound class method
                                    UnboundMethodType               Unbound class method
    模块                            ModuleType                      模块
    类                              ClassType                       类定义
    类实例                          InstanceType                    类实例
    文件                            FileType                        文件对象
    内部类型                        CodeType                        字节编译码
                                    FrameType                       执行框架
                                    TracebackType                   异常的堆栈跟踪
                                    SliceType                       由扩展切片操作产生
                                    EllipsisType                    在扩展切片中使用
    5,列表的方法
    方法                    描述
    list(s )                把序列s转换为一个列表
    s.append(x)             把一个元素添加到列表的结尾,相当于` s[len(s):] = [x]`
    s.extend(t)             将链表 t 的所有元素添加到 s 的末尾来扩充列表 s,相当于 `s[len(s):] = t`
    s.count(x)              返回值 x 在列表 s 中出现的次数
    s.index(x)              返回列表s中第一个值为 x 的元素的索引值
    s.insert(i,x)           在 s[i] 前插入一个元素 x
    s.pop([i])              返回 s[i] 的值并将 s[i] 元素从列表中删除。如果 i 被省略,` s.pop()` 就对最后一个元素进行操作。
    s.remove(x )            删除列表中值为 x 的第一个元素
    s.reverse()             翻转 s 中的全部元素
    s.sort([cmpfunc ])      对列表 s 中的元素进行排序,cmpfunc 是一个可选的比较函数
    6,字符串方法
    方法                                    描述
    s.capitalize()                          第一个字母变大写
    s.count(sub [,start [,end ]])           子串sub出现的次数
    s.encode([encoding [,errors ]])         改变字符串的编码
    s.startswith(prefix [,start [,end ]])   检查字符串的开头是否为prefix
    s.endswith(suffix [,start [,end ]])     检查字符串的结尾是否是suffix
    s.expandtabs([tabsize ])                将制表符转换为一定数量的空格
    s.find(sub [,start [,end ]])            返回子串 sub 首次出现的位置或者 -1
    s.rfind(sub [,start [,end ]])           返回子串 sub 末次出现的位置或者 -1
    s.index(sub [,start [,end ]])           返回子串 sub 首次出现的位置或者引起异常
    s.rindex(sub [,start [,end ]])          返回子串 sub 末次出现的位置或者引发异常
    s.isalnum()                             字符是否都为字母或数字
    s.isalpha()                             字符是否都为字母
    s.isdigit()                             字符是否都为数字
    s.islower()                             字符是否都为小写
    s.isspace()                             字符是否都为空白
    s.istitle()                             检查字符是否为标题格式(每个单词的第一个字母大写)
    s.isupper()                             字符是否都为大写
    s.join(t)                               用 s 连接 t 中的所有字符串
    s.center(width)                         在长度为 width 范围内将字符串置中
    s.ljust(width )                         在宽度为 width 内左对齐
    s.rjust(width )                         在宽度为 width 内右对齐
    s.lower()                               s 中所有字符小写
    s.upper()                               s 中所有字符大写
    s.replace(old , new [,maxreplace ])     将子串 old 替换为 new
    s.lstrip()                              删去字符串s开头的空白
    s.rstrip()                              删去字符串s末尾的空白
    s.strip()                               删去字符串s开头和末尾的空白
    s.split([sep [,maxsplit ]])             将字符串 s 分割成一个字符串列表,其中 sep 为分隔符,maxsplit是最大分割次数
    s.splitlines([keepends ])               将字符串按行分割为一个字符串列表,若keepends为1,则保留换行符'\n'
    s.swapcase()                            串内字符大写变小写,小写变大写,没有大小写的不变
    s.title()                               s 转换为标题格式(每个单词的第一个字母大写)
    s.translate(table [,deletechars ])      使用字符转换表转换一个字符串
    7,映射对象的方法和操作
    项目                    描述
    len(m)                  返回m中的条目个数
    m[k]                    返回关键字k索引的元素
    m[k] = x                设置关键字k索引的值为x
    del m[k]                删除一个元素
    m.clear()               删除所有元素
    m.copy()                返回m的一个浅拷贝
    m.has_key(k)            若 m 中存在 key k 返回True,否则返回False
    m.items()               返回包含所有关键字和对应值(key ,value )的列表
    m.keys()                返回由所有关键字组成的列表
    m.update(b)             将字典b中的所有对象加入m
    m.values()              返回一个包含m中所有对应值的列表
    m.get(k[,v])            返回m[k],若m[k]不存在时,返回 v
    m.setdefault(k[,v])     返回m[k],若m[k]不存在时,返回 v 并设置m[k] = v
    m.popitem()             从 m 中随机删除一个元素,并以元组的形式返回其关键字和值
    8,用户定义函数 f 有如下属性:
    属性                                    描述
    f.__module__                            函数定义所在的模块名
    f.__doc__ 或 f.func_doc                 文档字符串
    f.__name__ 或 f.func_name               函数名 (从2.4版开始该属性由只读变为可写)
    f.__dict__ 或 f.func_dict               支持任意函数属性的函数名字空间
    f.func_code                             (函数编译后产生的)字节码
    f.func_defaults                         包含所有默认参数的元组
    f.func_globals                          函数所在模块的全局名称空间的字典(只读)
    f.func_closure                          None or a tuple of cells that contain bindings for the function's free variables. Read-only
    9,方法对象的属性:
    属性                      描述
    m.im_self               引用类实例对象,如果是非绑定方法,im_self通常为 None(见下面小注)
    m.im_func               引用类中定义的方法对象
    m im_class              引用定义该方法的类
    m.__doc__               等于 m.im_func.__doc__
    m.__name__              等于 m.im_func.__name__
    m.__module__            等于 m.im_func.__module__

    小注: 当一个用户定义方法引用的是一个类方法时,不论是否绑定到类实例,它的 im_self属性都等于其 im_class 属性。
    10,内建函数及内建方法
    属性            方法
    b.__doc__       文档字符串
    b.__name__      函数/方法名
    b.__self__      方法所绑定的实例(未绑定时,返回None)
    b.__members__   方法的属性名(返回列表)
    11,模块对象拥有的属性
    属性            描述
    m.__dict__      保存模块名字空间的字典
    m.__doc__       模块的文档字符串
    m.__name__      模块名字
    m.__file__      模块的文件名
    m.__path__      当一个模块通过一个包被引用时,__path__是包的名字
    12,class对象定义的属性
    属性            描述
    c.__dict__      类 c 的名字空间
    c.__doc__       类 c 的文档字符串
    c.__name__      类 c 的名字
    c.__module__    类 c 的定义所在的模块
    c.__bases__     类 c 的所有父类(这是一个元组)
    13,一个代码对象 c 拥有如下只读属性
    属性                    描述
    c.co_argcount           参数的个数(不包括 * 或 ** 参数)
    c.co_code               原始字节码字符串
    c.co_consts             字节代码用到的常量
    c.co_filename           对象 c 所在的文件
    c.co_firstlineno        被编译源代码第一行行号
    c.co_flags              解释器标志: 1=优化 | 2=newlocals | 4=*arg | 8=**arg
    c.co_lnotab             源代码行号=>字节码偏移量 这是一个映射字典
    c.co_name               该代码对象的名字
    c.co_names              字节代码用到的局部变量名 这是一个元组
    c.co_nlocals            字节代码用到的局部变量个数
    c.co_stacksize          需要的虚拟机堆践大小(包含内部变量)
    c.co_varnames           一个元组,包括全部的局部变量名和参数名
    14,Frame 对象拥有如下只读属性
    属性                    描述
    f.f_back                下一个外部frame对象(对当前frame的调用者来说) 如果已到栈底的话 它的值就是 None
    f.f_code                当前frame中正在执行的代码对象
    f.f_locals              当前frame可见的局部变量的字典
    f.f_globals             当前frame可见的全局变量的字典
    f.f_builtins            当前frame可见的内建名字的字典
    f.f_restricted          是否在受限模式下运行 0:不受限 | 1:受限
    f.f_lineno              源代码当前行号
    f.f_lasti               字节码当前指令索引
    可改变的属性:
    f.f_trace               当前frame的跟踪函数(供调试器使用) 或 None
    f.f_exc_type            当前frame发生的异常类型 或 None
    f.f_exc_value           当前frame发生的异常的值 或 None
    f.f_exc_traceback       当前framev发生的 traceback 或 None
    15,traceback 对象
    属性                    描述
    t.tb_next               栈追踪的下一级 (对发生异常的 frame 来说) 或 None
    t.tb_frame              当前级正在执行的 frame 对象
    t.tb_lineno             引发异常的源代码行号
    t.tb_lasti              正在执行的指令索引
    16,对象创建,删除,表示使用的特殊方法
    方法                               描述
    __init__(self[,args])              初始化self
    __del__(self)                      删除self
    __repr__(self)                     创建self的规范字符串表示
    __str__ (self)                     创建self的信息字符串表示
    __cmp__(self,other)                比较两个对象,返回负数,零或者正数
    __hash__(self)                     计算self的32位哈希索引
    __nonzero__(self)                  真值测试,返回0或者1
    17,访问属性的方法
    方法                                    描述
    __getattr__(self , name)                返回属性 self.name
    __setattr__(self , name , value)        设置属性 self.name = value
    __delattr__(self , name)                删除属性 self .name
    18,序列和映射的方法
    方法                                        描述
    __len__(self)                               返回self的长度 len(someObject) 会自动调用 someObject的__len__()
    __getitem__(self , key)                     返回self[key]
    __setitem__(self , key , value)             设置self[key] = value
    __delitem__(self , key)                     删除self[key]
    __getslice__(self ,i ,j)                    返回self[i:j]
    __setslice__(self ,i ,j ,s)                 设置self[i:j] = s
    __delslice__(self ,i ,j)                    删除self[i:j]
    __contains__(self ,obj)                     返回 obj 是否在 self 中
    19,数学操作的方法
    Method                          Result
    __add__(self ,other)            self + other
    __sub__(self ,other)            self - other
    __mul__(self ,other)            self * other
    __div__(self ,other)            self / other
    __mod__(self ,other)            self % other
    __divmod__(self ,other)         divmod(self ,other)
    __pow__(self ,other [,modulo]) self ** other , pow(self , other , modulo)
    __lshift__(self ,other)         self __rshift__(self ,other)         self >> other
    __and__(self ,other)            self & other
    __or__(self ,other)             self | other
    __xor__(self ,other)            self ^ other
    __radd__(self ,other)           other + self
    __rsub__(self ,other)           other - self
    __rmul__(self ,other)           other * self
    __rdiv__(self ,other)           other / self
    __rmod__(self ,other)           other % self
    __rdivmod__(self ,other)        divmod(other ,self)
    __rpow__(self ,other)           other ** self
    __rlshift__(self ,other)        other __rrshift__(self ,other)        other >> self
    __rand__(self ,other)           other & self
    __ror__(self ,other)            other | self
    __rxor__(self ,other)           other ^ self
    __iadd__(self ,other)           self += other
    __isub__(self ,other)           self -= other
    __imul__(self ,other)           self *= other
    __idiv__(self ,other)           self /= other
    __imod__(self ,other)           self %= other
    __ipow__(self ,other)           self **= other
    __iand__(self ,other)           self &= other
    __ior__(self ,other)            self |= other
    __ixor__(self ,other)           self ^= other
    __ilshift__(self ,other)        self __irshift__(self ,other)        self >>= other
    __neg__(self)                   -self
    __pos__(self)                   +self
    __abs__(self)                   abs(self)
    __invert__(self)                ~self
    __int__(self)                   int(self)
    __long__(self)                  long(self)
    __float__(self)                 float(self)
    __complex__(self)               complex(self)
    __oct__(self)                   oct(self)
    __hex__(self)                   hex(self)
    __coerce__(self ,other)         Type coercion
    20,比较方法
    方法                            操作
    __lt__(self ,other )            self __le__(self ,other )            self __gt__(self ,other )            self > other
    __ge__(self ,other )            self >= other
    __eq__(self ,other )            self == other
    __ne__(self ,other )            self != other
    21,内建数据类型使用的内存大小
    类型              大小

    Integer                 12 bytes
    Long integer            12 bytes + (nbits/16 + 1)*2 bytes
    Floats                  16 bytes
    Complex                 24 bytes
    List                    16 bytes + 4 bytes(每个元素)
    Tuple                   16 bytes + 4 bytes(每个条目)
    String                  20 bytes + 1 byte(每个字符)
    Unicode string          24 bytes + 2 bytes(每个字符)
    Dictionary              24 bytes + 12*2n bytes, n = log2(nitems)+1
    Class instance          16 bytes 加一个字典对象
    Xrange object           24 bytes
    22,数值操作
    运算                    描述
    x + y                  
    x - y                  
    x * y                  
    x / y                   常规除
    x // y                  地板除
    x ** y                  乘方 (xy )
    x % y                   取模 (x mod y )
    -x                      改变操作数的符号位
    +x                      什么也不做
    ~x                      ~x=-(x+1)
    23,位运算符
    操作                    描述
    x x >> y                  右移
    x & y                   按位与
    x | y                   按位或
    x ^ y                   按位异或 (exclusive or)
    ~x                      按位翻转
    24,内建函数支持所有的数值类型
    函数                    描述
    abs(x )                 绝对值
    divmod(x ,y )           返回 (int(x / y ), x % y )
    pow(x ,y [,modulo ])    返回 (x ** y ) x % modulo
    round(x ,[n])           四舍五入,n为小数点位数
    25,序列运算
    操作                      描述
    s + r                   序列连接
    s * n , n * s           s的 n 次拷贝,n为整数
    s % d                   字符串格式化(仅字符串)
    s[i]                    索引
    s[i :j ]                切片
    x in s , x not in s     从属关系
    for x in s :            迭代
    len(s)                  长度
    min(s)                  最小元素
    max(s)                  最大元素
    26,字符串格式转换
    字符              输出格式
    d,i             十进制整数或长整数
                  无符号十进制整数或长整数
                  八进制整数或长整数
                  十六进制整数或长整数
                  十六进制整数或长整数(大写字母)
                  浮点数如 [-]m.dddddd
                  浮点数如 [-]m .dddddde ±xx .
                  浮点数如 [-]m .ddddddE ±xx .
    g,G             指数小于-4或者更高精确度使用 %e 或 %E; 否则,使用 %f
                  字符串或其他对象,使用str()来产生字符串
                  与 repr() 返回的字符串相同
                  单个字符
                  转换符标识 %
    27,字典的操作
    操作              描述
    x = d[k ]       通过 key 访问字典元素
    d [k ] = x      通过 key 对字典元素进行赋值
    del d[k ]       通过 key 删除某个字典元素
    len(d )         字典的元素个数
    28,增量赋值语句
    操作            等价表达式
    x += y          x = x + y
    x -= y          x = x - y
    x *= y          x = x * y
    x /= y          x = x / y
    x **= y         x = x ** y
    x %= y          x = x % y
    x &= y          x = x & y
    x |= y          x = x | y
    x ^= y          x = x ^ y
    x >>= y         x = x >> y
    29,类型转换
    函数                      描述
    int(x [,base ])         将x转换为一个整数
    long(x [,base ])        将x转换为一个长整数
    float(x )               将x转换到一个浮点数
    complex(real [,imag ])  创建一个复数
    str(x )                 将对象 x 转换为字符串
    repr(x )                将对象 x 转换为表达式字符串
    eval_r(str )              用来计算在字符串中的有效Python表达式,并返回一个对象
    tuple(s )               将序列 s 转换为一个元组
    list(s )                将序列 s 转换为一个列表
    chr(x )                 将一个整数转换为一个字符
    unichr(x )              将一个整数转换为Unicode字符
    ord(x )                 将一个字符转换为它的整数值
    hex(x )                 将一个整数转换为一个十六进制字符串
    oct(x )                 将一个整数转换为一个八进制字符串
    30,Unicode编码参数
    值                               描述
    'ascii'                         7-bit ASCII
    'latin-1' or 'iso-8859-1'       ISO 8859-1 Latin-1
    'utf-8'                         8-位可变长度编码
    'utf-16'                        16-位可变长度编码(可能是 little endian或 big endian)
    'utf-16-le'                     UTF-16, little-endian 编码
    'utf-16-be'                     UTF-16, big-endian 编码
    'unicode-escape'                与Unicode文字 u"string" 相同
    'raw-unicode-escape'            与原始 Unicode文字 ur"string"相同
    31,布尔表达式
    操作符             描述
    x or y          如果 x 为假,返回 y ; 否则,返回 x
    x and y         如果 x 为假,返回 x ; 否则,返回 y
    not x           如果 x 为假,返回 True ; 否则,返回 False
    32,运算优先级
    运算                                      名称
    --------------------------------------------------------------------
    (...), [...], {...}                     创建元组,列表,字典
    --------------------------------------------------------------------
    `...`                                   字符串转换
    --------------------------------------------------------------------
    s[i ], s[i:j ],  .attr                  索引,切片,属性
    --------------------------------------------------------------------
    f(...)                                  函数调用s
    --------------------------------------------------------------------
    +x , -x , ~x                            一元运算符
    --------------------------------------------------------------------
    x ** y                                  乘方(从右至左运算)
    --------------------------------------------------------------------
    x * y , x / y , x % y                   乘,除,取模
    --------------------------------------------------------------------
    x + y , x - y                           加,减
    --------------------------------------------------------------------
    x > y                         移位
    --------------------------------------------------------------------
    x & y                                   按位与
    --------------------------------------------------------------------
    x ^ y                                   按位异或
    --------------------------------------------------------------------
    x | y                                   按位或
    --------------------------------------------------------------------
    x x > y , x >= y ,
    x == y , x != y                         比较,身份,序列成员检测
    y
    x is y , x is not y
    x in s , x not in s
    --------------------------------------------------------------------
    not x                                   逻辑非
    --------------------------------------------------------------------
    x and y                                 逻辑与
    --------------------------------------------------------------------
    x or y                                  逻辑或
    --------------------------------------------------------------------
    lambda args : expr                      lambda函数表达式

     

     

     

  • 相关阅读:
    Leetcode#145 Binary Tree Postorder Traversal
    Leetcode#146 LRU Cache
    单引号和双引号的区别
    $* $@ $#
    pthread_detach
    pthread_join
    intent 启动activity、service的方法
    multicast based on udp
    ARM指令系统
    ARM寄存器
  • 原文地址:https://www.cnblogs.com/eagleking0318/p/6521325.html
Copyright © 2011-2022 走看看