美文网首页
time,random模块

time,random模块

作者: 全宇宙最帅De男人 | 来源:发表于2018-02-10 19:49 被阅读0次

time模块

1.time.time() 时间戳
2.time.sleep(3) 程序在此处停留3s
3.time.clock() 计算CPU执行时间
4.time.gmtime() utc时间,struct_time结构化时间:(tm_year=* ,...)
5.time.localtime() 本地时间
6.time.strftime(format,p_tuple)

    %Y : year
    %m : month
    %d : day
    %H : hour
    %M : minute
    %S : second

7.time.strptime(string,format) 结构化时间
8.time.ctime() 根据时间戳显示时间,时间格式固定
9.time.mktime() 根据时间转换成时间戳
** datetime模块 **
datetime.datetime.now() 正常格式的时间

random模块

1.random.random() 0-1之间随机数
2.random.randint(1,8) 1-8之间随机数,包括8
3.random.choice(序列) 序列中随机元素
4.random.sample(序列,int) 序列中随机选择int个元素
5.random.randrange(1,8) 1-8随机,不包括8
6.产生5位字母数字组成的随机验证码

    import random
    def v_code():
        code=''
        for i in range(5):

            add = random.choice([random.randrange(10),chr(random.randrange(65,91))])

            code+=str(add)
        print(code)

    v_code()

算法一:字符串反转

    #字符串切片,格式[start:end:step]
    def func1(s):
        return ''.join(s[::-1])
    #递归
    def func2(s):
        if len(s) < 1:
            return s
        return func2(s[1:])+s[0]
    #使用栈
    def func3(s):
        l=list(s)
        result=''
        while len(l)>0:
            result+=l.pop()
        return result
    #for循环-->enumerate()枚举,可以返回可迭代对象中每个元素和对应的索引
    def func4(s):
        result=''
        max_index=len(s)-1
        for index,value in enumerate(s):
            result += s[max_index-index]
        return result
    #使用reduce
    from functools import reduce
    def func5(s):
        result=reduce(lambda x,y:y+x,s)
        return result

留给大家一个问题(答案后台留言):

    def func6(s):
        lst=list(s)
        result=''.join(lst.reverse())
        return result
    print(func6('hello world!'))

1.运行结果是什么?
2.怎么改进?
提示:lst.reverse()返回NoneType
运行结果是什么

相关文章

  • time,random模块

    time模块 1.time.time() 时间戳2.time.sleep(3) 程序在此处停留3s3.time.c...

  • 常用的模块

    1.time、datetime 时间模块2.random 随机模块3.os ...

  • 模块汇总

    标准库 sys模块random模块os模块讲解time模块turtle模块讲解 数据可视化 matplotlib模...

  • 模块 一

    json与pickle序列化模块 time与datetime时间模块 random随机模块 hashlib 校验模...

  • 2.系统模块上

    time时间模块 datetime模块 随机模块random demo: 取随机验证码 解决路径引用问题 os模...

  • 其他模块

    1,time 和 datetime 模块 2,random模块 3,os模块 os模块是与操作系统交互的一个接口 ...

  • python的time模块&random模块& os模块

    1.time模块(★★★) 在Python中,通常有这几种方式来表示时间:1.时间戳(timestamp) :通常...

  • 案例9:与电脑PK小游戏

    题目:与电脑PK小游戏题目知识点:1-知识点:time模块、random模块、for循环、while循环、格式化字...

  • 7.2 import用法,random、time模块

    导入模块实际操作 模块重命名 import ...as..... 导入部分功能 from...import ran...

  • random模块和numpy.random模块用法总结

    python中的random模块 numpy模块的random模块 numpy.random.rand(d0, d...

网友评论

      本文标题:time,random模块

      本文链接:https://www.haomeiwen.com/subject/uidhtftx.html