zoukankan      html  css  js  c++  java
  • Python 基础【二】 上

    一、python语言分类

      1. C python  

      c语言的python版本 官方推荐 使用C语言实现,使用最为广泛,CPython实现会将源文件(py文件)转换成字节码文件(pyc文件),然后运行在Python虚拟机上。

    2. Jython  java python

      Python语言的Java实现,不仅提供Python的库,同时也提供所有的Java类。能运行在任何可兼容的Java1.1或更高的Java虚拟机平台上。

    3.IronPython

        Python的C#实现,IronPython将Python代码编译成C#字节码,然后在CLR上运行。(与Jython类似)。

    4.PyPy

      Python实现的Python,将Python的字节码字节码再编译成机器码。pypy与其他版本对比,编译过程相对慢,但是执行效率是最高的,因为它是编译成机器码,cpu可以直接执行机器码,其他语言都需要再转换成机器码,然后交由cpu执行。

    二、接收传值及模块导入

    导入模块:import sys

    接收参数:

    argv必须导入sys
    1 import sys
    2 print(sys.argv)

    通过 python gwd_sys.py,屏幕打印输出

        ['hello_args.py']

    通过 python gwd_sys.py my son屏幕打印输出

        ['hello_args.py', 'my', 'son']

    注:返回类型为列表

    三、 内容编码

      针对2.x版本

      python解释器加载py文件时,会对内容进行编码(默认ascii)

      ASCII(American Standard Code for Information Interchange,美国标准信息交换代码)是基于拉丁字母的一套电脑编码系统,

      主要用于显示现代英语和其他西欧语言。它是现今最通用的单字节编码系统,并等同于国际标准ISO/IEC 646。

      在计算机中,所有的数据在存储和运算时都要使用二进制数表示(因为计算机用高电平和低电平分别表示1和0)

      ASCII 码使用指定的7 位或8 位二进制数组合来表示128 或256 种可能的字符。标准ASCII 码也叫基础ASCII码,使用7 位二进制数来

      表示所有的大写和小写字母,数字0 到9、标点符号, 以及在美式英语中使用的特殊控制字符。其中:
      0~31及127(共33个)是控制字符或通信专用字符(其余为可显示字符),

      如控制符:LF(换行)、CR(回车)、FF(换页)、DEL(删除)、BS(退格)、BEL(响铃)等;

      通信专用字符:SOH(文头)、EOT(文尾)、ACK(确认)等;

      ASCII值为8、9、10 和13 分别转换为退格、制表、换行和回车字符。

      它们并没有特定的图形显示,但会依不同的应用程序,而对文本显示有不同的影响。
      32~126(共95个)是字符(32是空格),

      其中48~57为0到9十个阿拉伯数字。
      65~90为26个大写英文字母,

      97~122号为26个小写英文字母,

      其余为一些标点符号、运算符号等。

      

    标准I表

    Bin
    Dec
    Hex
    缩写/字符
    解释
    0000 0000
    0
    00
    NUL(null)
    空字符
    0000 0001
    1
    01
    SOH(start of headline)
    标题开始
    0000 0010
    2
    02
    STX (start of text)
    正文开始
    0000 0011
    3
    03
    ETX (end of text)
    正文结束
    0000 0100
    4
    04
    EOT (end of transmission)
    传输结束
    0000 0101
    5
    05
    ENQ (enquiry)
    请求
    0000 0110
    6
    06
    ACK (acknowledge)
    收到通知
    0000 0111
    7
    07
    BEL (bell)
    响铃
    0000 1000
    8
    08
    BS (backspace)
    退格
    0000 1001
    9
    09
    HT (horizontal tab)
    水平制表符
    0000 1010
    10
    0A
    LF (NL line feed, new line)
    换行键
    0000 1011
    11
    0B
    VT (vertical tab)
    垂直制表符
    0000 1100
    12
    0C
    FF (NP form feed, new page)
    换页键
    0000 1101
    13
    0D
    CR (carriage return)
    回车键
    0000 1110
    14
    0E
    SO (shift out)
    不用切换
    0000 1111
    15
    0F
    SI (shift in)
    启用切换
    0001 0000
    16
    10
    DLE (data link escape)
    数据链路转义
    0001 0001
    17
    11
    DC1 (device control 1)
    设备控制1
    0001 0010
    18
    12
    DC2 (device control 2)
    设备控制2
    0001 0011
    19
    13
    DC3 (device control 3)
    设备控制3
    0001 0100
    20
    14
    DC4 (device control 4)
    设备控制4
    0001 0101
    21
    15
    NAK (negative acknowledge)
    拒绝接收
    0001 0110
    22
    16
    SYN (synchronous idle)
    同步空闲
    0001 0111
    23
    17
    ETB (end of trans. block)
    传输块结束
    0001 1000
    24
    18
    CAN (cancel)
    取消
    0001 1001
    25
    19
    EM (end of medium)
    介质中断
    0001 1010
    26
    1A
    SUB (substitute)
    替补
    0001 1011
    27
    1B
    ESC (escape)
    换码(溢出)
    0001 1100
    28
    1C
    FS (file separator)
    文件分割符
    0001 1101
    29
    1D
    GS (group separator)
    分组符
    0001 1110
    30
    1E
    RS (record separator)
    记录分离符
    0001 1111
    31
    1F
    US (unit separator)
    单元分隔符
    0010 0000
    32
    20
    (space)
    空格
    0010 0001
    33
    21
    !
     
    0010 0010
    34
    22
    "
     
    0010 0011
    35
    23
    #
     
    0010 0100
    36
    24
    $
     
    0010 0101
    37
    25
    %
     
    0010 0110
    38
    26
    &
     
    0010 0111
    39
    27
    '
     
    0010 1000
    40
    28
    (
     
    0010 1001
    41
    29
    )
     
    0010 1010
    42
    2A
    *
     
    0010 1011
    43
    2B
    +
     
    0010 1100
    44
    2C
    ,
     
    0010 1101
    45
    2D
    -
     
    0010 1110
    46
    2E
    .
     
    00101111
    47
    2F
    /
     
    00110000
    48
    30
    0
     
    00110001
    49
    31
    1
       
    00110010
    50
    32
    2
       
    00110011
    51
    33
    3
       
    00110100
    52
    34
    4
       
    00110101
    53
    35
    5
       
    00110110
    54
    36
    6
       
    00110111
    55
    37
    7
       
    00111000
    56
    38
    8
       
    00111001
    57
    39
    9
       
    00111010
    58
    3A
    :
       
    00111011
    59
    3B
    ;
       
    00111100
    60
    3C
    <
       
    00111101
    61
    3D
    =
       
    00111110
    62
    3E
    >
       
    00111111
    63
    3F
    ?
       
    01000000
    64
    40
    @
       
    01000001
    65
    41
    A
       
    01000010
    66
    42
    B
       
    01000011
    67
    43
    C
       
    01000100
    68
    44
    D
       
    01000101
    69
    45
    E
       
    01000110
    70
    46
    F
       
    01000111
    71
    47
    G
       
    01001000
    72
    48
    H
       
    01001001
    73
    49
    I
       
    01001010
    74
    4A
    J
       
    01001011
    75
    4B
    K
       
    01001100
    76
    4C
    L
       
    01001101
    77
    4D
    M
       
    01001110
    78
    4E
    N
       
    01001111
    79
    4F
    O
       
    01010000
    80
    50
    P
       
    01010001
    81
    51
    Q
       
    01010010
    82
    52
    R
       
    01010011
    83
    53
    S
       
    01010100
    84
    54
    T
       
    01010101
    85
    55
    U
       
    01010110
    86
    56
    V
       
    01010111
    87
    57
    W
       
    01011000
    88
    58
    X
       
    01011001
    89
    59
    Y
       
    01011010
    90
    5A
    Z
       
    01011011
    91
    5B
    [
       
    01011100
    92
    5C
       
    01011101
    93
    5D
    ]
       
    01011110
    94
    5E
    ^
       
    01011111
    95
    5F
    _
       
    01100000
    96
    60
    `
       
    01100001
    97
    61
    a
       
    01100010
    98
    62
    b
       
    01100011
    99
    63
    c
       
    01100100
    100
    64
    d
       
    01100101
    101
    65
    e
       
    01100110
    102
    66
    f
       
    01100111
    103
    67
    g
       
    01101000
    104
    68
    h
       
    01101001
    105
    69
    i
       
    01101010
    106
    6A
    j
       
    01101011
    107
    6B
    k
       
    01101100
    108
    6C
    l
       
    01101101
    109
    6D
    m
       
    01101110
    110
    6E
    n
       
    01101111
    111
    6F
    o
       
    01110000
    112
    70
    p
       
    01110001
    113
    71
    q
       
    01110010
    114
    72
    r
       
    01110011
    115
    73
    s
       
    01110100
    116
    74
    t
       
    01110101
    117
    75
    u
       
    01110110
    118
    76
    v
       
    01110111
    119
    77
    w
       
    01111000
    120
    78
    x
       
    01111001
    121
    79
    y
       
    01111010
    122
    7A
    z
       
    01111011
    123
    7B
    {
       
    01111100
    124
    7C
    |
       
    01111101
    125
    7D
    }
       
    01111110
    126
    7E
    ~
       
    01111111
    127
    7F
    DEL (delete)
    删除
     
    八进制
    十六进制
    十进制
    字符
    八进制
    十六进制
    十进制
    字符
    0
    0
    0
    nul
    100
    40
    64
    @
    1
    1
    1
    soh
    101
    41
    65
    A
    2
    2
    2
    stx
    102
    42
    66
    B
    3
    3
    3
    etx
    103
    43
    67
    C
    4
    4
    4
    eot
    104
    44
    68
    D
    5
    5
    5
    enq
    105
    45
    69
    E
    6
    6
    6
    ack
    106
    46
    70
    F
    7
    7
    7
    bel
    107
    47
    71
    G
    10
    8
    8
    bs
    110
    48
    72
    H
    11
    9
    9
    ht
    111
    49
    73
    I
    12
    0a
    10
    nl
    112
    4a
    74
    J
    13
    0b
    11
    vt
    113
    4b
    75
    K
    14
    0c
    12
    ff
    114
    4c
    76
    L
    15
    0d
    13
    cr
    115
    4d
    77
    M
    16
    0e
    14
    so
    116
    4e
    78
    N
    17
    0f
    15
    si
    117
    4f
    79
    O
    20
    10
    16
    dle
    120
    50
    80
    P
    21
    11
    17
    dc1
    121
    51
    81
    Q
    22
    12
    18
    dc2
    122
    52
    82
    R
    23
    13
    19
    dc3
    123
    53
    83
    S
    24
    14
    20
    dc4
    124
    54
    84
    T
    25
    15
    21
    nak
    125
    55
    85
    U
    26
    16
    22
    syn
    126
    56
    86
    V
    27
    17
    23
    etb
    127
    57
    87
    W
    30
    18
    24
    can
    130
    58
    88
    X
    31
    19
    25
    em
    131
    59
    89
    Y
    32
    1a
    26
    sub
    132
    5a
    90
    Z
    33
    1b
    27
    esc
    133
    5b
    91
    [
    34
    1c
    28
    fs
    134
    5c
    92
    35
    1d
    29
    gs
    135
    5d
    93
    ]
    36
    1e
    30
    re
    136
    5e
    94
    ^
    37
    1f
    31
    us
    137
    5f
    95
    _
    40
    20
    32
    sp
    140
    60
    96
    '
    41
    21
    33
    !
    141
    61
    97
    a
    42
    22
    34
    "
    142
    62
    98
    b
    43
    23
    35
    #
    143
    63
    99
    c
    44
    24
    36
    $
    144
    64
    100
    d
    45
    25
    37
    %
    145
    65
    101
    e
    46
    26
    38
    &
    146
    66
    102
    f
    47
    27
    39
    `
    147
    67
    103
    g
    50
    28
    40
    (
    150
    68
    104
    h
    51
    29
    41
    )
    151
    69
    105
    i
    52
    2a
    42
    *
    152
    6a
    106
    j
    53
    2b
    43
    +
    153
    6b
    107
    k
    54
    2c
    44
    ,
    154
    6c
    108
    l
    55
    2d
    45
    -
    155
    6d
    109
    m
    56
    2e
    46
    .
    156
    6e
    110
    n
    57
    2f
    47
    /
    157
    6f
    111
    o
    60
    30
    48
    0
    160
    70
    112
    p
    61
    31
    49
    1
    161
    71
    113
    q
    62
    32
    50
    2
    162
    72
    114
    r
    63
    33
    51
    3
    163
    73
    115
    s
    64
    34
    52
    4
    164
    74
    116
    t
    65
    35
    53
    5
    165
    75
    117
    u
    66
    36
    54
    6
    166
    76
    118
    v
    67
    37
    55
    7
    167
    77
    119
    w
    70
    38
    56
    8
    170
    78
    120
    x
    71
    39
    57
    9
    171
    79
    121
    y
    72
    3a
    58
    :
    172
    7a
    122
    z
    73
    3b
    59
    ;
    173
    7b
    123
    {
    74
    3c
    60
    <
    174
    7c
    124
    |
    75
    3d
    61
    =
    175
    7d
    125
    }
    76
    3e
    62
    >
    176
    7e
    126
    ~
    77
    3f
    63
    ?
    177
    7f
    127
    del

        

      大小规则

      1)数字0~9比字母要小。如"7"<"F";

      2)数字0比数字9要小,并按0到9顺序递增。如"3"<"8"

      3)字母A比字母Z要小,并按A到Z顺序递增。如"A"<"Z"

      4)同个字母的大写字母比小写字母要小。如"A"<"a"。      

      记住几个常见字母的ASCII码大小:

      “换行LF”为0x0A;“回车CR”为0x0D;空格为0x20;"0"为0x30; "A"为0x41;"a"为0x61。另外还有128-255的ASCII字符。

      查询ASCII技巧,方便查询ASCII码对应的字符:新建一个文本文档,按住ALT+要查询的码值(注意,这里是十进制)

      松开即可显示出对应字符。例如:按住ALT+97,则会显示出'a'。

       扩展 ASCII

      扩展ASCII 字符是从128 到255(0x80-0xff)的字符。扩展ASCII不再是国际标准。

      汉字编码        

      0-127 是7位ASCII 码的范围,是国际标准。至于汉字,不同的字符集用的ascii 码的范围也不一样,常用的汉字字符集有GB2312-80,GBK,Big5,unicode 等。

      GB_2312 字符集是目前最常用的汉字编码标准,windows 95/98/2000 中使用的 GBK字符集 就包含了GB2312,或者说和GB2312 兼容,GB_2312 字符集包含了 6763个的 简体汉字,和682 个标准中文符号。在这个标准中,每个汉字用2个字节来表示,每个字节的ascii码为 161-254,(16 进制A1 - FE),第一个字节 对应于 区码的1-94 区,第二个字节 对应于位码的1-94 位。161-254 其实很好记忆,大家知道英文字符中,可打印的字符范围为33-126。将这对数加上128(或者说最高位置1),就得到汉字使用的字符的围。GB18030的规范是汉字第一个字节在0x81-0xFE之间,第二个字节位于区间0x40-0x7E以及0x80-0xFE。每个字节转化为整数大于128。

     

     四、字符串

      禁忌:

        用"+"的方式拼接字符串会大大浪费内存空间,因为他每+ 1次就会在内存中开辟一个新的空间

    推荐:
    name = 'python' print("%s come" % name) print("{name} world".format(name = name)) print("{0} world".format(name)) print("{0} world,{1}".format(name,"I like python"))

    五、数据类型

    查看内置方法

    (一)数字

        int

      1. 3.x

    ['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']

      2. 2.x


    ['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__float__', '__floordiv__', '__format__', '__getattribute__', '__getnewargs__', '__hash__', '__hex__', '__index__', '__init__', '__int__', '__invert__', '__long__', '__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'imag', 'numerator', 'real']

      

    stum = -1
    print(stum.__abs__())
    
    # D:Pythonpython.exe D:/untitled1/se.py
    1
    __abs__ 绝对值
    stum = 5
    print(stum.__add__(6))
    加法 __add__

    不列出具体方法了,查看内置函数的方法:

        按ctrl+数据类型,就可以查看具体方法,如:

    def __abs__(self, *args, **kwargs): # real signature unknown
            """ abs(self) """
            pass
    
        def __add__(self, *args, **kwargs): # real signature unknown
            """ Return self+value. """
            pass
    
        def __and__(self, *args, **kwargs): # real signature unknown
            """ Return self&value. """
            pass
    
    def 是定义函数  __abs__函数名
    self代表对象本身
    
    这样就可以了解一个函数需要哪些功能,大致功能是什么


      2.7  long

    ['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__float__', '__floordiv__', '__format__', '__getattribute__', '__getnewargs__', '__hash__', '__hex__', '__index__', '__init__', '__int__', '__invert__', '__long__', '__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'imag', 'numerator', 'real']

      注:3.x中已经没有long 长整形

      float:

      

    ['__abs__', '__add__', '__bool__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getformat__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__int__', '__le__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__pos__', '__pow__', '__radd__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rmod__', '__rmul__', '__round__', '__rpow__', '__rsub__', '__rtruediv__', '__setattr__', '__setformat__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', 'as_integer_ratio', 'conjugate', 'fromhex', 'hex', 'imag', 'is_integer', 'real']
    3.5 float

    (二)字符串 str

      

    python3.x
    dir(str)
    #['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
    
    #python2.7
    dir(str)
    #['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

     常用方法

    str_test = "你好"
    print(str_test.__add__("python"))
    __add__ 连接字符串
    str_test = "你好"
    print(str_test.__contains__(""))
    
    """
    有返回True
    没有指定回False
    """
    __contains__ 是否包含
    str_test = "你好"
    print(str_test.__eq__("你好"))
    
    print(str_test.__eq__("K"))
    __eq__ 比较

     capitalize() 首字母大写

    str_test = "ACcdefg"
    print(str_test.casefold())
    casefold() 全部转换成小写
  • 相关阅读:
    Safari-IoS调试
    前端加密技术
    gulp入门
    xss攻击
    xml 解析
    python 基本语法
    python初识
    字节
    神奇的算式
    linux-虚拟机安装
  • 原文地址:https://www.cnblogs.com/sunface/p/5157127.html
Copyright © 2011-2022 走看看