Python中的各个模块(进阶)
1. python math 模块
import math
print(math.ceil(4.1))
print(math.floor(4.9))
print(math.fabs(-10))
print(math.sqrt(9))
print(math.exp(1))
**5
4
10.0
3.0
2.718281828459045**
2. python random 模块
1. random的随机数
import random
random= random.random()
print(random)
0.9693881604049188
2. 输出两个随机数使它一样
import random
random.seed(500)
ss=random.random()
print(ss)
random.seed(500)
ss=random.random()
print(ss)
0.7973375876367403
0.7973375876367403
3. randint()生成一个随机整数
ran = random.randint(1,20)
print(ran)
17
4. # 随机选取0到100间的偶数:
import random
print random.randrange(0, 101, 2)
22
5. # 随机浮点数:
import random
print(random.uniform(1,20))
12.507026123895775
6. # 随机字符串:
import random
print (random.choice(['你好','好','你','呀']))
呀
7. # 多个字符中生成指定数量的随机字符:
import random
print (random.sample('asdfghklbnvbn',6))
['k', 'f', 'g', 'a', 'j', 's']
8. #随机选取字符串:
import random
print (random.choice(['剪刀', '石头', '布']))
[石头]
9. #打乱排序:
import random
shufu=[1,2,3,54,546,1216,126]
random.shuffle(shufu)
print (shufu)
[54, 126, 2, 3, 1, 1216, 546]
字符串
a=nihao
b=python
print(a+b)
nihaopython
1.字符串的截取
a='nihao'
print(a[1:4])
iha
|