模块包含 (类,方法,语句)
print('-------------导入模块的所有---------------------------')
import math #关于数学运算
print(id(math))
print(type(math))
print(math)
print(math.pi)
print('----------------------------------------')
print(dir(math))
print(math.pow(2,3),type(math.pow(2,3)))
print(math.ceil(9.001))
print(math.floor(9.9999))
print('-------------导入模块的pi方法- -------------------------')
from math import pi
print(pi)
自定义模块并导入
自定义calc.py 模块
def add(a,b):
return a+b
def div(a,b):
return a/b
#如何导入自定义模块
import calc
print(calc.add(10,20))
print(calc.div(10,4))
#导入自定义模块的 add方法
from calc import add
print(add(10,20))
主程序的运行方式
#创建模块calc2.py
def add(a,b):
return a+b
if __name__ == '__main__':
print(add(10,20)) #只有当点击运行calc2时,才会执行运算, 别的方法调用不运行此语句
# 导入创建的模块
import calc2
print(calc2.add(100,200))
包的概念
# 包里面包括模块
'''
创建pageage1包 在报下创建 两个模块 module_A, module_B
module_A.py
a=10
module_B.py
b=20
'''
#在模块中导入 pageage1包
import pageage1.module_A as ma #ma是pageage1.module_A这个模块的别名
#print(pageage1.module_A.a)
print(ma.a)
print-------------------包的本质-------------------------)
#导入带有包的模块时注意事项
import pageage1
import calc
#使用import方式进行导入时,只能跟包名或模块名
from pageage1 import module_A
from pageage1.module_A import a
#使用from ...import可以导入包,模块,函数,变量.
内置模块
'''
os 提供访问操作系统服务功能的标准库
calendar 提供与日期相关的各种函数的标准库
urllib 用于读取来自网上(服务器)的数据标准库
json 用于使用JSON序列化和反序列话对象
re 用于在字符串中执行正则表达式匹配和替换
decimal 用于进行精确控制运算精度,有效数位和四舍五入操作的十进制运算
logging 提供了灵活的记录事件,错误
'''
import sys # 与python解释器及其环境操作相关的标准库
import time # 提供与时间相关的各种函数的标准库
import urllib.request
import math # 提供标准算术运算函数的标准库
print(sys.getsizeof(24))
print(sys.getsizeof(45))
print(sys.getsizeof(True))
print(sys.getsizeof(False))
print(time.time())
print(time.localtime(time.time()))
print(urllib.request.urlopen('http://www.baidu.com').read())
print(math.pi)
####### schedule 模块的操作
cmd --- pip install schedule
# 导入
import schedule
import time
def job():
print('哈哈 -------')
schedule.every(3).seconds.do(job)
while True:
schedule.run_pending()
time.sleep(1)