常用模块

  • calendar
  • time
  • datetime
  • timeit
  • os
  • shutil
  • zip
  • math
  • string
  • 上述所有模块使用理论上都应该先导入,string是特例
  • calendar,time,datetime的区别参考中文意思

random

  • 随机数
  • 所有的随机模块都是伪随机
import random
# random() 获取0-1之间的随机小数
#  格式:random.random()
#  返回值:随机0-1之间的小数

print(random.random())
0.6909718929087878

 

# choice() 随机返回序列中的某个值
#  格式:random.choice(序列)
#  返回值:序列中的某个值

l = [str(i)+"haha" for i in range(10)]
print(l)
rst = random.choice(l)
print(rst)
['0haha', '1haha', '2haha', '3haha', '4haha', '5haha', '6haha', '7haha', '8haha', '9haha']
5haha

 

# shuffle() 随机打乱列表
#  格式:random.shuffle(列表)
#  返回值:打乱顺序之后的列表

l1 = [i for i in range(10)]
print(l1)

random.shuffle(l1)
print(l1)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[4, 9, 0, 5, 3, 7, 8, 2, 1, 6]

 

# randint(a,b): 返回一个a到b之间的随机整数,包含a和b

print(random.randint(0,100))

71