总结
随机整数
import random
# 1.随机整数
# random.randint(m, n) - 产生m 到 n的随机整数
print(random.randint(10, 20))
随机浮点数
# random.random() - 产生0到1的随机小数 1取不到
print(random.random()) # [0, 1)
print(random.random() * 100) # [0, 100)
print(random.random() * 80 + 20) # [20, 100)
随机打乱
# random.shuffle() - 随机打乱列表中元素的顺序
# random.shuffle(列表)
nums = [1, 2, 39, 23, 45, 51]
random.shuffle(nums)
print(nums)
随机获取序列中的一个元素
# random.chioce(序列) - 随机获取序列中的一个元素
# random.choices(序列, k=n) - 随机获取序列中n个元素
nums = [1, 2, 39, 23, 45, 51]
print(random.choice(nums))
print(random.choices(nums, k=2))
# import uuid - 产生一个唯一文本信息
import uuid
print(uuid.uuid1())
# print(uuid.uuid3())
print(uuid.uuid4())
# print(uuid.uuid5())
时间相关的模块
import time
- 时间戳
"""
时间戳是通过保存某个时间点到1970年1月1日0时0分0秒(格林威治时间)的时间差来保存时间的,单位是秒
保存时间戳比保存字符串时间更节约内存
"""
# 1.time.time() - 获取当前时间的时间戳
t1 = time.time()
print(t1)
# 2.
# time.localtime() - 获取当前当地时间,返回的是结构体时间
# time.localtime(时间戳) - 将时间戳转换成结构体时间
t2 = time.localtime()
print(t2)
print(t2.tm_year)
t3 = time.localtime(0)
print(t3)
# 3.time.sleep() - 睡眠指定时间
# print('===============')
# time.sleep(2)
# print('--------------------')
- datetime模块
from datetime import datetime, time, date, timedelta
# 1.获取当前时间
t1 = datetime.now()
print(t1, type(t1)) # 2021-05-14 16:45:05.335201 <class 'datetime.datetime'>
print(t1.year, t1.month, t1.day, t1.hour) # 2021 5 14 19
t2 = date.today()
print(t2) # 2021-05-14
# 2.获取时间对应的时间戳
# datetime对象.timestamp() - 获取时间对象对应的时间戳
t3 = t1.timestamp()
print(t3) # 1620992511.741626
# 3.将字符串时间转换成时间对象
# datetime.strptime(时间字符串, 时间格式字符串)
t_str = '2015-3-4 18:30:25'
t4 = datetime.strptime(t_str, '%Y-%m-%d %H:%M:%S')
print(t4, type(t4)) # 2015-03-04 18:30:25 <class 'datetime.datetime'>
print(t4.timestamp()) # 1425465025.0
t_str2 = '2010/8/4'
t5 = datetime.strptime(t_str2, '%Y/%m/%d')
print(t5) # 010-08-04 00:00:00
print(t5.timestamp()) # 1280851200.0
# 4.日期的加减操作
t_str3 = '2015-12-31 23:59:59'
t6 = datetime.strptime(t_str3, '%Y-%m-%d %H:%M:%S')
print(t6 + timedelta(seconds=1)) # 2016-01-01 00:00:00
print(t6 + timedelta(days=10)) # 2016-01-10 23:59:59
print(t6 - timedelta(days=1, hours=1)) # 2015-12-30 22:59:59
- 包的使用
# 1.什么是包
# 包就是包含__init__.py文件的文件夹。包是专门用来对模块进行封装的。
# 2.包的使用(导入包中的模块、或者导入包中的模块的内容)
"""
a. import 包.模块
b. import 包.模块 as 新名字
c. from 包 import 模块1, 模块2,...
d. from 包.模块 import 变量, 变量2,...
"""
# 导入方式一:
import fileManager.excelFile
fileManager.excelFile.read_col()
# 导入方式二:
import fileManager.excelFile as excelFile
excelFile.read_line()
# 导入方式三:
from fileManager import excelFile, jsonFile
excelFile.read_col()
jsonFile.write_json_file()
# 导入方式四:
from fileManager.textFile import read_file, open_file
read_file()
open_file()
# 3. 导入包中的内容的原理
# 通过包导入模块或者模块内容的时候,系统会先执行包中__init__.py文件,然后再执行对应的模块。
# from fileManager.excelFile import read_col
import fileManager
fileManager.open_file()
fileManager.close_file()
from fileManager import open_file, close_file
open_file()
close_file()
- 模块的使用
# 1.什么是模块
# python中一个py就是一个模块
# 2.怎么在一个模块中使用另一个模块中的内容(内容包括:数据,函数,类)
"""
注意:1.如果要在一个模块中,使用另外一个模块中的内容,被使用的模块的模块名必须是标识符,且不是关键字
2.一个模块中只有全局变量才能被其他模块使用
3.其他模块只有在被导入以后才能被使用
"""
# 3.怎么导入模块
"""
1.import 模块名 - 导入指定模块,导入后可以通过'模块名.变量' 的方式使用被导入的模块中所有的全局变量
2.from 模块名 import 变量1, 变量2, 变量3, ... - 导入指定模块中的指定变量,导入后直接使用指定变量
3.from 模块名 import * - 导入指定模块中所有的变量,导入直接使用
4.重命名
1)import 模块名 as 新模块名 - 给模块重命名,重命名后,使用新的名字来代替原模块名
2)from 模块名 import 变量1 as 新变量1, 变量2, 变量3, ... - 给变量重命名
"""
# import 模块名
# 一
# import test
# print(test.a)
# print(test.name)
# test.test()
# 方式二
# from test import a, name, test
# print(a)
# print(name)
# test()
# from functools import reduce
# reduce()
# import functools
# functools.reduce()
# 方式三
# from test import *
# print(a)
# print(name)
# test()
# 方式四:重名的情况,有变量名和模块名重名的情况
# test = 100
# print(f'test{test}')
#
# import test as test_module
# print(test_module.a)
# print(test_module.name)
# test_module.test()
# a = 200
# name = 'xiaoming'
# from test import name as new_name_var, a as new_a
# print(name, new_name_var, new_a + a)
# 4.导入模块的原理
"""
执行代码的时候遇到import或者 from-import 导入模块的时候,系统会进入指定模块,
将模块中的代码全部执行一遍
"""
# import test
# from test import a
# 4.导入模块的原理
"""
执行代码的时候遇到import或者 from-import 导入模块的时候,系统会进入指定模块,
将模块中的代码全部执行一遍
"""
# import test
# from test import a
# 5.重复导入
"""
python 导入模块的时候会自动检测被导入到模块是否已经导入过,如果导入过不会重复导入
(在任何位置,需要任何模块的时候直接导,不需要有任何重复导入的负担)
"""
# 这个if语句中的代码在模块直接运行的时候会执行,在被别的模块导入的时候不会执行
# 原理:每个模块中都有一个__name__属性,用来保存当前模块的模块名,
# __name__的默认值是py文件的文件名
# 当执行运行某个模块的时候,这个模块__name__会自动变成'__main__',
# 其他没有被执行运行的模块的__name__都是文件名。
# if __name__ == '__main__':
# # 代码块