zoukankan      html  css  js  c++  java
  • python学习笔记1:数字相关操作

    1. 绝对值

    >>> abs(-5.0)  
    5.0  
    

    2. 数字取整

    2.1. 只取整数部分int()

    int(), 直接截去小数部分,剩下整数部分, 返回int型数;

    >>> int(5.0) # 5  
    >>> int(5.1) # 5  
    >>> int(5.9) # 5  
    >>> int(6.0) # 6  
    >>>  
    >>> int(-5.0) # -5  
    >>> int(-5.1) # -5  
    >>> int(-5.9) # -5  
    >>> int(-6.0) # -6  
    

    2.2. 向下取整math.floor()

    math.floor() 向下取整,返回int型数;

    >>> math.floor(5.0) # 5  
    >>> math.floor(5.1) # 5  
    >>> math.floor(5.9) # 5  
    >>> math.floor(6.0) # 6  
    >>>  
    >>> math.floor(-5.0) # -5,小于等于-5.0 的最大整数是-5;  
    >>> math.floor(-5.1) # -6,小于等于-5.1 的最大整数是-6;  
    >>> math.floor(-5.9) # -6,小于等于-5.9 的最大整数是-6;  
    >>> math.floor(-6.0) # -6,小于等于-6.0 的最大整数是-6;  
    

    2.3. 向上取整 math.ceil()

    math.ceil() 向上取整,返回int型数;

    >>> math.ceil(5.0) # 5  
    >>> math.ceil(5.1) # 6  
    >>> math.ceil(5.9) # 6  
    >>> math.ceil(6.0) # 6  
    >>>  
    >>> math.ceil(-5.0) # -5,大于等于-5.0 的最小整数是-5;  
    >>> math.ceil(-5.1) # -5,大于等于-5.1 的最大整数是-5;  
    >>> math.ceil(-5.9) # -5,大于等于-5.9 的最大整数是-5;  
    >>> math.ceil(-6.0) # -6,大于等于-6.0 的最大整数是-6;  
    

    2.4. 四舍五入round()

    round(number[, ndigits]) 四舍五入,如果两边整数一样远,刚返回偶数一边;
    参数ndigits表示小数位的数目;如果ndigits=0,返回int型数,如果ndigits>=1, 返回float型数;

    >>> round(-2.5) # -2  
    >>> round(-1.5) # -2  
    >>> round(-0.5) # 0  
    >>> round( 0.5) # 0  
    >>> round( 1.5) # 2  
    >>> round( 2.5) # 2  
       
    >>> round(1.05, 1) # 1.1 本应该靠偶数一边是1.2, 但由于内部存储原因,返回1.1;  
    #1.05  
    # + 0.0000_0000_0000_0010  
    # -----------------------  
    # = 1.0500_0000_0000_0012  
    # 所以1.05更靠近1.1 这边;  
       
       
    >>> round(1.15, 1) # 1.1 本应该靠偶数一边是1.2, 但由于内部存储原因,返回1.1;  
    #1.15  
    # + 0.0000_0000_0000_0002  
    # -----------------------  
    # = 1.1500_0000_0000_0001  
    # 所以1.05更靠近1.1 这边;  
       
       
    >>> round(1.25, 1) # 1.2  
    >>> round(1.35, 1) # 1.4  
    >>> round(1.45, 1) # 1.4  
    >>> round(1.55, 1) # 1.6  
    >>> round(1.65, 1) # 1.6  
       
    >>> round(2.675, 2) # 2.67, 本应该靠偶数一边是2.68, 但由于内部存储原因,返回2.67;  
    

    在python2.7的doc中,round()的最后写着,"Values are rounded to the closest multiple of 10 to the power minus ndigits; if two multiples are equally close, rounding is done away from 0."保留值将保留到离上一位更近的一端(四舍六入),如果距离两端一样远,则保留到离0远的一边。所以round(0.5)会近似到1,而round(-0.5)会近似到-1。

    但是到了python3.5的doc中,文档变成了"values are rounded to the closest multiple of 10 to the power minus ndigits; if two multiples are equally close, rounding is done toward the even choice."如果距离两边一样远,会保留到偶数的一边。比如round(0.5)和round(-0.5)都会保留到0,而round(1.5)会保留到2。

    round(2.675, 2) 的结果,不论我们从python2还是3来看,结果都应该是2.68的,结果它偏偏是2.67,为什么?这跟浮点数的精度有关。我们知道在机器中浮点数不一定能精确表达,因为换算成一串1和0后可能是无限位数的,机器已经做出了截断处理。那么在机器中保存的2.675这个数字就比实际数字要小那么一点点。这一点点就导致了它离2.67要更近一点点,所以保留两位小数时就近似到了2.67。

    3. 乘方、开方

    pow(x, y) # 求x的y次方;

    >>> pow(3, 2) # 9,求平方,返回int型数;  
    >>> pow(3, 1/2) # 1.7320508075688772, 1/2次方即开方运算;  
    >>> pow(4, 1/2) # 2.0, 平方根返回的是float型数;  
    

    math.pow(),与pow()类似

    >>> math.pow(3, 2) # 9.0, 返回的是float型数  
    

    对于求平方根,除了可以使用math.pow(x, 1/2)外,还有专门的平方根函数math.sqrt(x)

    >>> math.sqrt(2) # 1.4142135623730951,求平方根  
    >>> math.sqrt(4) # 2.0, 平方根返回的是float型数;  
    

    4. 三角函数、对数

    三角函数

    >>> math.pi # 3.1415_9265_3589_793, 使用math.pi可以调用π;  
    >>> math.sin(math.pi / 6) # 0.4999_9999_9999_9999_4,得到的并不是0.5;  
    >>> math.cos(math.pi / 3) # 0.5000_0000_0000_0001,得到的并不是0.5;  
    >>> math.tan(math.pi / 4) # 0.9999_9999_9999_9999,得到的并不是1.0;  
    

    对数,math.log(A, B=e) 以B为底,A的对数,默认以e为底;

    >>> math.e             # 2.7182_8182_8459_045,自然常数e;
    >>> math.log(100, 10) # 2.0,返回的是float型数;
    >>> math.log(math.e)  # 1.0,
    

    5. 随机数

    5.1. 随机整数

    randint(a, b),[a, b]之间的整数,包含b;

    >>> import random  
    >>> random.randint(10, 20) # 16,返回[10, 20]之间的随机整数;算个bug吧  
    >>> random.randint(20, 10) # 报错ValueError,参数对顺序有要求;  
    >>> random.randint(10, 10) # 10,上下限相同,返回这个数;  
    

    randrange(start, stop=None, step=1, _int=<class 'int'>), 不包含stop

    >>> random.randrange(10, 20, 2) # 返回[10, 20)之间的偶数,不包括stop  
    

    5.2. 随机浮点数

    uniform(a, b), [a, b]之间的浮点数,包含b;

    >>> import random  
    >>> random.uniform(10, 20) # 12.132xxxxx,返回[10, 20]之间的随机浮点数;  
    >>> random.uniform(20, 10) # 10.414xxxxx,uniform(),两个参数不关心顺序;  
    >>> random.uniform(10, 10) # 10.0,上下限相同,返回这个数;  
    

    random.random(), [0, 1)之间的浮点数,不包含1;不需要参数

    >>> random.random()  
    0.1862385***  
    

    5.3. 在待选中随机选择

    语法:random.choice(seq)
    参数:seq可以是列表、元组、字符串
    返回:返回一个元素

    >>> import random  
    >>> random.choice([1, 3, 5]) # 从list中随机选择一个  
    3  
    >>> random.choice('abc!@#') # 从str中随机选择一个  
    #  
    

    语法:random.sample(seq, n)
    参数:seq,列表、元组、字符串;n,数目;
    返回:返回一个列表

    >>> import random  
    >>> random.sample([1, 3, 5, 7, 9], 3) # 从list中随机选择三个  
    [5, 1, 3]  
    >>> random.sample('abc!@#') # 从str中随机选择三个  
    ['a', '#', 'b']  
    

    5.4. 打乱序列

    语法:random.shuffle(x, random=None)
    参数:x,一个list;random,??
    返回:返回None,传入的lst会被改变

    >>> import random  
    >>> L0 = [1, 2, 3, 4, 5]  
    >>> random.shuffle(L0) # 将L0洗牌, 该语句返回None  
    >>> L0  
    [4, 5, 3, 1, 2]  
    

    6. 平均值/方差/标准差

    需要第三方模块 numpy

    import numpy as np
    
    f_mean = np.mean(_list) # 平均值
    f_var = np.var(_list) # 方差
    f_std = np.std(_list, ddof=1) # 标准差
    

    7. 进制转换

    7.1. 使用bin/oct/hex、int

    7.1.1. 十进制-> 其它进制

    >>> #使用bin/oct/hex,输入int,返回str;
    >>> bin(50) # '0b110010',十进制 -> 二进制,返回的值为字符串,以0b开头;
    >>> oct(50) # '0o62',十进制 -> 八进制,返回的值为字符串,以0o开头;
    >>> hex(50) # '0x32', 十进制 -> 十六进制,返回的值为字符串,以0x开头;
    

    7.1.2. 其它进制-> 十进制

    >>> s_bin = '110010'# 二进制str;
    >>> i_int= int(str(s_bin), 2) # 50, 输入二进制str,返回十进制整数;
    >>>
    >>> i_int = int(str(62), base=8)# 50,输入八进制str,返回十进制整数;
    >>> i_int = int(str(32), base=16) # 50,输入十六进制str,返回十进制整数;
    

    7.2. 使用format

    缺点是format的参数只能是各进制的整数,不能是str;
    所有需要把参数使用int()处理后再使用format, 如:
    '{n:08b}'.format(n=int(s0, base=16))

    7.2.1. 转为二进制

    >>> '{:b}'.format(13)      #输入十进制整数,输出二进制str,:b表示转为二进制;
    '1101'  
    >>> '{:08b}'.format(13)    #输入十进制整数,输出二进制str,:08b表示转为二进制,宽度为8,不足补0;  
    '00001101'  
    >>> '{:08b}'.format(0x000D)#输入十六进制整数,输出二进制str;  
    '00001101'  
    >>> s0='0xd'  
    >>> '{n:08b}'.format(n=int(s0, base=16)) #输入十六进制str, 转为二进制, 中间需要经过int()处理  
    '00001101'  
    

    7.2.2. 转为八进制

    >>> '{:o}'.format(13)   #输入十进制整数,输出八进制str,:o表示转为八进制;  
    '15'  
    >>> '{:08o}'.format(13) #输入十进制整数,输出八进制str,:08o表示转为八进制,宽度为8,不足补0;  
    '00000015'  
    >>> '{:08b}'.format(0xD)#输入十六进制整数,输出八进制str;  
    '00000015'  
    

    7.2.3. 转为十六进制

    使用大写的:X, 可以转出的16进制字符.

    >>> '{:x}'.format(13)   #输入十进制整数,输出十六进制str,:x表示转为十六进制
    'd'  
    >>> '{:04x}'.format(13) #输入十进制整数,输出十六进制str,:04x表示转为十六进制,宽度为4,不足补0
    '000d'  
    >>> '{:04x}'.format(0b01101)#输入二进制整数,输出十六进制str;  
    '000d'  
    >>> '{:04x}'.format(int('0b01101', base=2)#输入二进制str,输出十六进制str;
    '000d'  
    
    
  • 相关阅读:
    STL中的distance和advance的简单用法
    Excel 根据数据 快捷生成sql语句
    vi | vim 用法
    常用windows 命令
    .NETCore3.0 + EFCore中使用Oracle报“ORA-12154: TNS:could not resolve the connect identifier specified"的错误处理
    CentOS虚拟机上安装java
    Eclipse快捷键
    Spring学习笔记
    ifconfig: command not found(CentOS专版,其他的可以参考)
    利用正则表达式截取带有嵌套方括号中最内层的字符串, 无论嵌套多少层始终要最里面的方括号的内容
  • 原文地址:https://www.cnblogs.com/gaiqingfeng/p/13228709.html
Copyright © 2011-2022 走看看