美文网首页
Python数据类型

Python数据类型

作者: 开罗酒吧 | 来源:发表于2017-05-17 19:38 被阅读0次

    Numbers:
    Python's built-in core data types are in some cases also called object types. There are four built-in data types for numbers:

    Integer
    Normal integers
    e.g. 4321
    Octal literals (base 8)
    A number prefixed by a 0 (zero) will be interpreted as an octal number
    example:

    >>> a = 010
    >>> print a
    8 Alternatively, an octal number can be defined with "0o" as a prefix: >>> a = o10
    >>> print a
    8
    

    Hexadecimal literals (base 16)
    Hexadecimal literals have to be prefixed either by "0x" or "0X".
    example:

    >>> hex_number = 0xA0F
    >>> print hex_number
    2575
    

    Long integers
    these numbers are of unlimited size
    e.g.42000000000000000000L

    Floating-point numbers
    for example: 42.11, 3.1415e-10

    Complex numbers
    Complex numbers are written as <real part> + <imaginary part>j
    examples:

    >>> x = 3 + 4j
    >>> y = 2 - 3j
    >>> z = x + y
    >>> print z
    (5+1j)
    

    String:
    有三种方式定义字符串:
    1.'xxxxx'
    2."xxxxxx"
    3.'''xxxxxxxx'''

    字符串的一些操作:
    1."Hello"+"World"
    2."xx"*3 -> "xxxxxx"
    3."Python"[0] will return in "P"
    4."Python"[2:4]
    4.len("Python")

    字符串不可被改变

    If both a and b are strings, "a is b" checks if they have the same identity, i.e. share the same memory location. If "a is b" is True, then it trivially follows that "a == b" has to be True as well. But "a == b" True doesn't imply that "a is b" is True as well!

    >>> a = "Linux"
    >>> b = "Linux"
    >>> a is b
    True
    

    The Pitfalls of Repetitions

    In our previous examples we applied the repetition operator on strings and flat lists. We can apply it to nested lists as well:

    >>> x = ["a","b","c"]
    >>> y = [x] * 4
    >>> y
    [['a', 'b', 'c'], ['a', 'b', 'c'], ['a', 'b', 'c'], ['a', 'b', 'c']]
    >>> y[0][0] = "p"
    >>> y
    [['p', 'b', 'c'], ['p', 'b', 'c'], ['p', 'b', 'c'], ['p', 'b', 'c']]
    >>> 
    
    image.png

    complex(real[,imag]])
    创建一个值为real + imag * j的复数或者转化一个字符串或数为复数。如果第一个参数为字符串,则不需要指定第二个参数。
    参数real: int, long, float或字符串;
    参数imag: int, long, float。

    >>> complex(1, 2)
    (1 + 2j)
    #数字
    >>> complex(1)
    (1 + 0j)
    #当做字符串处理
    >>> complex("1")
    (1 + 0j)
    #注意:这个地方在“+”号两边不能有空格,也就是不能写成"1 + 2j",应该是"1+2j",否则会报错
    >>> complex("1+2j")
    (1 + 2j)
    

    相关文章

      网友评论

          本文标题:Python数据类型

          本文链接:https://www.haomeiwen.com/subject/uqfjxxtx.html