random:random模块用于产生随机数相关的功能
import random
print(random.random())#随机0-1之间的浮点数
print(random.randint(1,7))#前后都可以取到
print(random.randrange(1,4,2))#包含endpoint
range(1,3)#顾头不顾尾
print(random.choice((1,2,3)))#元组
print(random.choice('hello'))#字符串
print(random.choice(['a','b']))#列表
random.sample('hello',2)#从前面的任选两个输出
print(random.uniform(1,2))#取中间的浮点数
#随机洗牌
#l=[1,2,3,4,5,6]
#random.shuffle(l)
#print(l)
#随机四位数验证码代码:
checkcode=''#定义一个空的全局变零
for i in range(4):
current=random.randrange(0,4)
#字母
if current == i:
tmp=chr(random.randint(65,90))#ASCII码
#数字
else:
tmp=random.randint(0,9)
checkcode+=str(tmp)
print(checkcode)