标签:参数表 列表 bsp 1.2 print 应用 shuffle choice 组合
#random模块
import random
list1 = [1,3,5,7,9]
#print(random.random()) #0.09736432890033453 #(0,1)----float 大于0且小于1之间的小数
#print(random.randint(1,3)) # 1 ##[1,3] 大于等于1且小于等于3之间的整数
#print(random.randrange(1,3)) # 1 #[1,3) 大于等于1且小于3之间的整数
#print(random.choice([3,‘23‘,[4,5]])) #[4, 5] ##1或者23或者[4,5]
#print(random.sample([2,‘45‘,[‘ab‘],‘db‘],3)) #[[‘ab‘], 2, ‘db‘] ## 列表元素任意2个组合 #注意这里是有两个参数的,第二个参数表示几个元素组合
#print(random.uniform(1,3)) #1.2380799085529681 (1,3) 1-3之间的小数
# random.shuffle(list1) #shuffle() 打乱顺序
# print(list1) #[3, 9, 7, 5, 1]
#应用:生成随机的几位验证码
def v_code(n=5):
res = ‘‘
for i in range(n):
n = random.randint(0,9)
s = chr(random.randint(65,90))
num = random.choice([n,s])
res +=str(num)
return res
print(v_code()) #2269B
标签:参数表 列表 bsp 1.2 print 应用 shuffle choice 组合
原文地址:http://www.cnblogs.com/wangkc/p/6942533.html