Python3 学习第十弹: 模块学习三之数字处理

本文深入探讨Python中用于数学、科学计算的math、cmath、decimal、fractions和random等核心库的功能与应用,包括基础数学函数、复数运算、十进制处理、分数运算及随机数生成。
math模块
    提供基础的数学函数,
    cos(3.14) = -0.999..(弧度制) 
    acos(1) = 0.0 
    sqrt(9) = 3.0
    degrees(3.14) = 179.9999..(弧度转角度)
    radians(180) = 3.1415926..(角度转弧度)
    常量
    pi = 3.1415..
    e = 2.7182..
cmath模块(complex math, 复数)
    支持复数的运算,
    >>>import cmath
    >>>cmath.sqrt(-1)
    1j
    >>>(1+2j)*(3+4j)        # python 自身支持复数运算
    -5+10j
decimal模块
    提供一个十进制的小数处理方式,避免二进制浮点数的精度问题
    1> Decimal(x)
        将字符串或小数x转换为相应的decimal类型的小数,建议用字符串,若用小数依然有转换时的精度问题

    2> 各种基础的数学运算,如log(),compare(b),sqrt()

    3> 重载了+,-,*,/,**
fractions模块
    提供分数运算
    1> Fraction(a, b)
        将分数a/b转换为Fraction类型的分数
       Fraction(x)
           将小数x转换为相应Fraction的分数,可能不精确

    2> 重载了+,-,*,/,**
random模块
    用于生成随机数
    1> random()
        随机产生一个浮点数介于[0, 1)
    
    2> uniform(l, r)
        生成指定范围[l, r]内的随机浮点数

    3> randint(l, r)
        生成指定范围[l, r]内的随机整数

    4> randrange(start, stop = None[, step = 1])
        随机产生range(start, stop, step)中的随机数

    5> choice(sequence)
        随机从序列中取出一个元素,包括字符串,列表

    6> shuffle(x[, random])
        随机将一个列表中的元素顺序打乱

    7> sample(sequence, k)
        从序列中随机取出k个元素组成一个列表
time模块
    提供对于时间格式的操作
    1> time()
        返回一个以1970.1.1 00:00:00开始的秒数(时间戳)作为浮点数值返回
        >>> time.time()
        1413107005.683219
    
    2> ctime([seconds])
        返回标准化格式的时间,若有参数返回相应时间戳的格式化时间

    3> clock()
        返回处理器时钟时间,在UNIX系统上,它返回的是进程时间。

    4> localtime([seconds])
        将一个时间戳转化为当前时区的struct_time
        >>> time.localtime()
        time.struct_time(tm_year=2014, tm_mon=10, tm_mday=12, tm_hour=17, tm_min=58, tm_sec=22, tm_wday=6, tm_yday=285, tm_isdst=0)

    5> gmtime([seconds])
        将一个时间戳转化为UTC时区(0时区)的struct_time
        >>> time.gmtime()
        time.struct_time(tm_year=2014, tm_mon=10, tm_mday=12, tm_hour=9, tm_min=59, tm_sec=35, tm_wday=6, tm_yday=285, tm_isdst=0)

        对于struct_time类型,
        我们可以直接对其进行访问各个时间部分
        >>> time.gmtime()
        time.struct_time(tm_year=2014, tm_mon=10, tm_mday=12, tm_hour=9, tm_min=59, tm_sec=35, tm_wday=6, tm_yday=285, tm_isdst=0)
        >>> now = time.localtime()
        >>> now.tm_year
        2014
        >>> now.tm_hour
        18
        >>> now.tm_mday
        12

    6> mktime(struct_time)
        将一个struct_time类型转化为时间戳

    7> sleep(seconds)
        程序停止一定的时间运行,单位为秒

    8> asctime([struct_time])
        将一个struct_time转化为标准化格式的时间

    9> strftime(format[, struct_time])
        将一个struct_time转化为格式化的时间字符串
        >>> time.strftime('%Y.%m.%d %X')
        '2014.10.12 18:16:15'
        >>> time.strftime('%Y.%m.%d %H:%M:%S')
        '2014.10.12 18:17:54'

    10> strptime(string[, format])
        将一个时间字符串转化为struct_time
        默认字符串格式为 "%a %b %d %H:%M:%S %Y"

 

转载于:https://www.cnblogs.com/Mathics/p/4020944.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值