zoukankan      html  css  js  c++  java
  • Python3 学习第十弹: 模块学习三之数字处理

    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"
  • 相关阅读:
    AD 文档信息设置和制作模板
    用AT命令调试调制解调器
    语音AT命令参考
    传真AT指令部分(参考)
    传真调制解调器
    knockout+weui+zepto
    20171028随手记
    20171027随记
    一些简单又坑人的无聊使用
    ReadWrite,日志,输入文本
  • 原文地址:https://www.cnblogs.com/Mathics/p/4020944.html
Copyright © 2011-2022 走看看