zoukankan      html  css  js  c++  java
  • 《转》python数据类型

    转自 http://www.cnblogs.com/BeginMan/archive/2013/06/08/3125876.html

    一、标准类型函数

    cmp():比较大小

    str():转换为字符串

    type():类型

    cmp(...)
        cmp(x, y) -> integer
        Return negative(负数) if x<y, zero(0) if x==y, positive(正数) if x>y.

     如下:

    >>> cmp(5,3.2)
    1
    >>> cmp(3.5,8)
    -1

     二、转换工厂函数

    存在精度损失

    >>> int(1.847)
    1
    >>> long(42)
    42L
    >>> float(42)
    42.0
    >>> complex(42)
    (42+0j)
    >>> complex(2.4,-8)
    (2.3999999999999999-8j)

     三、功能函数

    用于数值运算:asb()、coerce()、divmod()、pow()、round()

    asb():absolute:绝对的;完全的;专制的;n:绝对值

    >>> abs(-1)
    1

     coerce():vt. 强制,迫使,
    类型转换,但是提供了不依赖python解释器而是通过自定义两个数值类型转换。返回一个元祖,存在强制行为。
    coerce(...)
        coerce(x, y) -> (x1, y1)
        
        Return a tuple consisting of the two numeric arguments converted to
        a common type, using the same rules as used by arithmetic operations.
        If coercion is not possible, raise TypeError.

    >>> coerce(1,2)
    (1, 2)
    >>> coerce(1.2,2l)
    (1.2, 2.0)
    >>> coerce(1.2,2)
    (1.2, 2.0)
    >>> coerce(1,2.3)
    (1.0, 2.2999999999999998)
    >>> coerce(1j,123)
    (1j, (123+0j))

     divmod():.divmod 整除求余、返回包含商和余数的元祖

    >>> divmod(10,3)
    (3, 1)
    >>> divmod(3,10)
    (0, 3)
    >>> divmod(10,2.5)
    (4.0, 0.0)

     pow():power of a number:指数的意思

    pow()与**都可以实现指数运算,pow()先出生些。

    >>> pow(2,5)
    32
    >>> 2**5
    32

     round():四舍五入

    round(...)
        round(number[, ndigits]) -> floating point number

        Round a number to a given precision in decimal digits (default 0 digits).
        This always returns a floating point number.  Precision may be negative.

    >>> round(1.234,2)
    1.23
    >>> round(3.14)
    3.0
    >>> for each in range(10):
             print round(math.pi,each)
         
    3.0
    3.1
    3.14
    3.142
    3.1416
    3.14159
    3.141593
    3.1415927
    3.14159265
    3.141592654

     四、仅用于整数的函数

    oct():octonary number system 八进制字符串形式

    >>> oct(255)
    '0377'

     hex():hexadecimal number system十六进制字符串形式

    >>> hex(255)
    '0xff'

     ASCII码转换函数

    ord():ordinal:序数,将字符转换成对应整数值

    >>> ord('A')
    65

     chr():char: 单个字符,数字对应当个ASCII字符

    >>> chr(65)
    'A'

     五、操作符

    >>> x>=80 and x<=100
    True
    >>> 80<=x<=100
    True
    -----------------------------
    总是写错:
    >>> 80=

     六、致用

    1、分数等级

    def result(x):
       dic={9:'A',8:'B',7:'C',6:'D'}
       myre=int(x)/10
       for obj in sorted(dic.keys(),reverse=True):   #True 和False 表示是否逆序
           if myre>= obj:
               out=dic[obj]
               break
           else:
               out='F'
       return out
     
    if __name__=="__main__":
        sorce = input('Enter your sorce:')

        print 'level:%s' %result(sorce) 

  • 相关阅读:
    return
    debug_backtrace final catch
    Exponential Backoff
    2014 MapReduce
    Extract, Transform, Load
    Factorization of a 768-bit RSA modulus
    奇偶校验 汉明码 如果一条信息中包含更多用于纠错的位,且通过妥善安排这些纠错位使得不同的出错位产生不同的错误结果,那么我们就可以找出出错位了。 crc CRC 循环冗余校验
    An Explanation of the Deflate Algorithm
    通过极限手续定义正数的无理指数方幂
    算术根的存在唯一性
  • 原文地址:https://www.cnblogs.com/cmybky/p/11772214.html
Copyright © 2011-2022 走看看