random模块
import random # 随机小数 print(random.random()) # 随机0-1小数 print(random.uniform(1,5)) # 随机自定义小数 # 随机整数 print(random.randint(1,2))# 随机整数,可以取到2 顾头顾尾 [1,2] print(random.randrange(1,2))# 随机整数,不可以取到2 顾头不顾尾 [1,2) print(random.randrange(1,9,2))# 随机整数,步长2 [1,3,5,7] # 随机抽取 l = ['a',1,(6,3),'abc'] # l1 = [1,2,3,'1'] print(random.choice(l)) # 随机抽取一个内容 print(random.sample(l,2))# 随机抽取几项内容 print(random.choices(l))# 随机抽取一项内容放入列表 # 打乱顺序 l = ['a',1,(6,3),'abc'] random.shuffle(l) print(l)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# 字母+数字验证码 方法1 import random super_str = [chr(i) for i in range(65,91)] low_str = [chr(i) for i in range(97,123)] num = [str(i) for i in range(10)] code = '' for i in range(6): code += random.choice(super_str+low_str+num) print(code)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# 字母+数字验证码 方法2 import random code = '' for i in range(6): rand_num = str(random.randint(0,9)) super_str = chr(random.randint(65,90)) low_str = chr(random.randint(97,122)) code += random.choice([rand_num,super_str,low_str]) print(code)