zoukankan      html  css  js  c++  java
  • Day 4-2 random模块

    1 import random
    2 random.randint(1,100)  # 从1到100中随机取出一个数.包含100
    3 random.randrange(1,100) #功能和上面一样.只是不包含100.
    4 random.random()         # 取一个随机浮点数.
    5 random.choice("123kldsjfoiauofjdlkcvjhasfhsafjr")  # 从字符串中随机返回一个字符
    6 random.sample("vajoure034zmvm2i0989383242u",4)  # 返回指定数量的字符串

    生成一个随机验证码:

    1 # 生成一个随机验证码,需要string模块
    2 import string
    3 string.ascii_letters  # 大小写字母
    4 string.digits        # 数字.
    5 string.punctuation   # 特殊字符
    6 s = string.ascii_letters + string.digits
    7 s1 = ''.join(random.sample(s,4))   # 生成一个4位的随机验证码.
    8 print(s1)

       打乱顺序:

    1 # 打乱顺序
    2 li = list(range(100))  # 生成一个列表
    3 random.shuffle(li)    #打乱列表中的顺序
    4 print(li)

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 

  • 相关阅读:
    python虚拟环境--virtualenv
    求导法则和高阶导数
    restful规范
    python协程--yield和yield from
    打印质数
    更改docker的存储位置
    同台ECS安装多个mysql
    jenkins + gitlab + harbor + k8s 部署项目
    jenkins 配置maven
    gitlab + jenkins 持续集成
  • 原文地址:https://www.cnblogs.com/lovepy3/p/8748530.html
Copyright © 2011-2022 走看看