• Python 基础之模块之math random time


    :math 数学模块
    import math
    #(1)ceil() 向上取整操作 (对比内置round)
    res = math.ceil(6.001)  #注意精度损耗
    print(res)
    #(2)floor()  向下取整操作 (对比内置round)
    res = math.floor(3.5)
    res = math.floor(3.999999999)
    print(res)
    #(3)pow() 计算一个数值的N次方(结果为浮点数)  (对比内置pow)
    res = math.pow(2,3)
    #res = math.pow(2,3,3) #math模块中的pow 只有2个参数
    print(res)
    #print(pow(2,3))
    #print(pow(2,3,5))
    #(4)sqrt() 开平方运算(结果浮点数)
    res = math.sqrt(9)
    print(res)
    #(5)fabs() 计算一个数值的绝对值(结果浮点数) (对比内置abs)
    res = math.fabs(-1)
    print(res)
    #(6)modf() 将一个数值拆分为正数和小数两部分组成元组
    res = math.modf(14.66)
    print(res)
    #(7)copysign() 将参数第二个数值的正负号拷贝给第一个
    res = math.copysign(-1234,22)
    print(res)
    #(8)fsum() 将一个容器数据中的数据进行求和运算(结果浮点数 ) (比对内置sum)
    listvar = [3,3123,3,31,43,21]
    res = math.fsum(listvar)
    print(res)

    :random 随机模块
    import random
    #(1)random() 获取随机0-1之间的小数(左闭右开)
    res = random.random()  #0<= x < 1
    print(res)
    #(2)randrange() 随机获取指定范围内的正数(包含开始值,不包含结束值,间隔值)
    res = random.randrange(2)  # 0 1
    print(res)
    res = random.randrange(1,6)  #1,2,3,4,5
    print(res)

    res = random.randrange(1,7,3) # 1 4
    print(res)
    #(3)randint() 随机产生指定范围内的随机正数
    #rangdint 目前是唯一一个高位值可以取得到的函数 (不推荐使用)
    res = random.randint(1,2)
    print(res)
    #res = rangdom.randint(2,6,2) #没有间隔值参数 功能不如randrange
    #print(res)
    #(4)uniform() 获取指定范围内的随机小数(左闭右开)
    res = random.uniform(2,4)  #z <= x < 4
    print(res)
    res = random.uniform(4,-2)
    print(res)
    #分析:
    a = 4, b= -2
    return a + (b-a) * self.random()
    4+(-2-4) * (0~1)
    4+-6*(0~1) =>  当取0时  4
    4+-6*(0~1) =>  当取1时  -2 (1是取不到的)
    所以:
    -2 < x <=4
    #(5)choice() 随机获取序列中的值(多选一)
    listvar = ["one","two","three","four"]
    res = random.choice(listvar)
    print(res)

    #自定义choice
    def mychoice():
        num = random.randrange(0,len(listvar))
        res = listvar[num]
        return res
    print(mychoice())
    #(6)sample() 随机获取序列中的值(多选多) [返回列表]
    listvar = ["one","two","three","four"]
    res = random.sample(listvar,2)
    print(res)
    #(7)shuffle() 随机打乱序列中的值(直接打乱原序列)
    listvar = ["one","two","three","four"]
    random.shuffle(listvar)
    print(listvar)
    #随机4位验证码
    def yanzhengma():
        strvar = ""
        for i in range(4):
            #产生大写字母A-Z
            bchr = chr(random.randrange(65,91))
            #产生小写字母a~z
            schr = chr(random.randrange(97,123))
            #数字0~9
            num = str(random.randrange(0,10))
            #把所有随机值得种类赛道列表里
            listvar = [bchr,schr,num]
            #随机选取一个
            res = random.choice(listvar)
            #拼接字符串
            strvar += res
        #返回字符串
        return strvar
    res = yanzhengma()
    print(res)

    :time 时间模块
    import time
    #(1)time() 获取本地时间戳
    res = time.time()
    print(res)
    #(2)mktime()  通过[时间元组]获取[时间戳] (参数是时间元组)
    ttp = (2019,5,16,10,55,30,0,0,0)
    print("=========")
    res = time.localtime()
    print(res)
    res = time.mktime(res)
    print(res)
    print("------------")
    #(3)localtime() 通过[时间戳] 获取[时间元组]  (默认当前时间)
    res = time.localtime()
    print(res)
    res = time.localtime(1557979012)
    print(res)
    #输出结果:
    time.struct_time
    (
    tm_year=2019,
    tm_mon=5,
    tm_mday=16,
    tm_hour=11,
    tm_min=56,
    tm_sec=52,
    tm_wday=3,
    tm_yday=136, t
    m_isdst=0
    )
    #(4)ctime() 通过[时间戳]回去[时间字符串] (默认当前时间)
    res = time.ctime()
    print(res)

    res = time.ctime(1557979012)
    print(res)
    #(5)asctime()  通过[时间元组]获取[时间字符串] (参数是时间元组)
    ttp = (2019,5,16,14,33,3,2,0,0)
    res = time.asctime(ttp)
    print(res)
    #(6)strftime()  通过[时间元组]格式化[时间字符串] (格式化字符串,[可选时间元组参数])
    res = time.strftime("%Y-%m-%d %H:%M:%S")
    print(res)
    ttp = (2019,5,16,14,33,3,2,0,0)
    res = time.strftime("%Y-%m-%d %H:%M:%S",ttp)
    print(res)
    #注意点Windows不支持strftime 的中文字符 linux完全可以
    #res = time.strftime("%Y-%m-%d %H:%M:%S 今天是谁的生日呢" )
    print(res)
    #(8)strptime()  通过[时间字符串]提取出[时间元组]  (时间字符串,格式化字符串)
    #注意点:strptime 在匹配字符串当中的时间时,字符串必须严丝合缝,不能随意修改原有字符.
    res = time.strptime("2019年5月17号,早上8点9分20秒的时候我们出去玩","%Y年%m月%d号早上%H点%M分%S秒")
    print(res)
    #输出:
    time.struct_time
    (
    tm_year=2019,
    tm_mon=5,
    tm_mday=17,
    tm_hour=8,
    tm_min=9,
    tm_sec=20,
    tm_wday=4,
    tm_yday=137,
    tm_isdst=-1
    )
    #(9)sleep()  程序睡眠等待
    #time.sleep(10)  #程序在此加阻塞,10秒之后在向下执行
    #print("继续向下执行")
    #(10)per_counter()   用于计算程序运行的时间
    #time.time()  一样可以实现
    print("=================")
    start_time = time.perf_counter()
    print(start_time)
    for i in range(100000000):
        pass
    end_time = time.perf_counter()
    print(end_time)
    res = end_time - start_time
    print(res)

     

  • 相关阅读:
    大数据方面核心技术有哪些
    ECS 挂载 OSS 多Bucket
    SqlServer 服务无法启动 操作系统错误: 5(拒绝访问。)
    java System类
    java Arrays类
    MySQL 高可用方案研究
    这个设计挺好的
    git强制提交命令
    React useEffect Hook的对象 & 数组依赖 useEffect监控复杂对象
    QQ浏览器基于chromeium70
  • 原文地址:https://www.cnblogs.com/hszstudypy/p/10888709.html
Copyright © 2020-2023  润新知