一、程序中有很多地方需要用到随机字符,比如登录网站的随机验证码,通过random模块可以很容易生成随机字符串
例如:>>> ''.join(random.sample(string.ascii_lowercase + string.digits, 6))
'4fvda1'
二、年会抽奖
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> random.shuffle(a)
>>> a
[8, 5, 2, 9, 3, 4, 7, 0, 1, 6]
>>> random.choice(a)
7