andom 模块
random.random()
返回 0 到 1 之间的一个随机小数
- >> random.random()0.15331915985695865>> random.random()0.6973041759495626
- >> round(random.random()*100)13>> str(random.random())[2:]'5091957416488744'
随机生成 100 以内的两位数
>> int(random.random()*100)11random.randint(a,b) 随机生成 a,b 之间的一个整数, 左右都是闭区间 >> random.randint(1,100)# 都是闭区间 10
random.randrange(a,b) 随机生成 a,b 之间的一个整数, 左开区间, 右闭区间
>> random.randrange(1,100)# 右边开区间 2
random.choice(seq) 随机返回序列中的一个元素值, 有返回值
>> random.choice("abc")# 字符串'a'>> random.choice(["a","b","c"])# 列表'c'>> random.choice(("a","b","c"))# 元组'b'
>> c = random.choice("abc")>> c'b'random.shuffle(list) 打乱列表 list 的元素顺序, 只能针对列表, 没有返回值 >> l = list(range(10))>> random.shuffle(l)>> l[0, 3, 6, 9, 1, 8, 2, 5, 4, 7]>> random.shuffle(l)>> l[8, 3, 7, 6, 4, 9, 0, 2, 5, 1]random.uniform()uniform()? 方法将随机生成下一个实数, 它在?[x, y)? 范围内. 返回一个浮点数, 有返回值
>> random.uniform(1,100)28.62268392118385>> f = random.uniform(1,3)>> f2.7439797786402718
random.sample(population,n) 在 population 中随机选取 n 个元素, 返回一个列表; population 可以是序列和集合, 包含列表, 元组, 字符串和集合
- >> import string>> random.sample(string.ascii_letters,3)# 字符串 ['f', 'a', 'X']
- >> l = list(range(1000))>> random.sample(l,5)# 列表 [776, 22, 715, 798, 982]
- >> t = (1,2,3,4,5,6,7,8,9,10)>> random.sample(t,4)# 元组 [3, 9, 2, 4]>> set1 = set(list(range(1000)))>> random.sample(set1,10)# 集合 [520, 405, 874, 301, 879, 597, 764, 428, 107, 268]
- >> result_list = random.sample(set1,10)>> print(result_list)[853, 218, 487, 955, 549, 990, 166, 557, 650, 615]
练习题: 1, 生成 9 位数字的密码一,
- #coding =utf-8
- import random
- numbers_passwd = ""
- for i in range(9):
- numbers_passwd += str(random.randint(0,9))
- print(numbers_passwd)
二,
- #coding =utf-8
- import random
- numbers_passwd = ""
- for i in range(9):
- numbers_passwd += "0123456789"[random.randint(0,9)]
- print(numbers_passwd)
2, 生成 9 位字母的密码
- #coding =utf-8
- import random
- import string
- letters_passwd = ""
- for i in range(9):
- letters_passwd += string.ascii_letters[random.randint(0,52)]
- print(letters_passwd)
3, 生成 9 位数字和字母的密码
- #coding =utf-8
- import random
- import string
- number_letter_passwd = ""
- for i in range(9):
- if random.randint(0,1):# 如果是随机生成的 1 生成一个字母密码元素
- number_letter_passwd += string.ascii_letters[random.randint(0,52)]
- else:# 否则生成数字密码元素
- number_letter_passwd += str(random.randint(0,9))
- print(number_letter_passwd)
sys.exit(1) 退出程序 #coding=utf-8import sysprint(1)sys.exit(1)print(2)
浮点数之坑
>> 10-9.90.09999999999999964>> 10100 - 9.910010.0>> (10100 - 9.9100)/1000.1
练习题: 1,10 进制数转换成 2 进制
- #coding =utf-8
- def devTobin(num):
- result = []
- while True:
- result.append(str(num%2))
- num = num//2
- if num ==0:
- break
- return "".join(result[::-1])
- print(devTobin(789))
3, 二进制转换为十进制
- 101 = 1*2^0 + 0*2^1 + 1*2^2 = 5
- #coding =utf-8
- def binTodev(binNum):
- result = 0
- number_length = len(binNum)
- for i in range(number_length):
- #print("i:",i)
- #print(binNum[i])
- result += int(binNum[-(i+1)])*pow(2,i)# 二进制的数要从右边开始取, 从 - 1 开始
- return result
- print(binTodev("1100010101"))
来源: http://www.bubuko.com/infodetail-2747241.html