import random print(random.random()) #0-1 只能是0的float print(random.randint(1,5)) #1-4 int print(random.randrange(1,3)) #1-2 print(random.choice([1,2,3,4,5,6])) #1-6 print(random.sample([1,2,3,4,5,6],2)) #1-6 随机获取2个值 print(random.uniform(1,3)) #1-2 可以是1-2的float
生成一个简单的5位验证码
def code(): key = "" for i in range(5): num = random.randint(0,9) s = random.randint(65,122) #A-Z a-z 中间91-96 是特符号 if s in [91,92,93,94,95,96]: #特殊符号 [ \\ ] ^ _ ` s = random.randint(65, 90) s = chr(s) cod = random.choice([num,s]) key += str(cod) print(key) code()