python学习(5)

2021-07-10 18:06

阅读:480

标签:def   元素   练习   练习题   元组   utf-8   result   shuff   shuf   

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)
11
random.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)
>> f
2.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-8
import sys
print(1)
sys.exit(1)
print(2)

浮点数之坑

>> 10-9.9
0.09999999999999964
>> 10100 - 9.9100
10.0
>> (10100 - 9.9100)/100
0.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"))

python学习(5)

标签:def   元素   练习   练习题   元组   utf-8   result   shuff   shuf   

原文地址:http://blog.51cto.com/13496943/2165981


评论


亲,登录后才可以留言!