@TOC
什么叫模块

模块的导入

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.999))


自定义模块导入,文件需mark为自定义模块

写入模块:
def add(a,b):
return a+b
def sub(a,b):
return a-b
def mul(a,b):
return a*b
def div(a,b):
return a/b
调用模块
# 导入calc自定义模块使用
import calc
print(calc.add(1,2))
以主程序的形式去运行


python中的包
1、包是一个分层次的目录结构,它将一组功能相近的模块组织在一个目录下
作用:
代码规范
避免模块名称冲突
包与目录的区别:
包是保用_init_.py文件的
包的导入:
import 包名.模块名

使用import方式进行导入的时候,只能跟包名或者模块名
使用from方式导入时:可以导入包,模块,函数,变量

python内置模块

import sys
import time
import urllib
print(sys.getsizeof(24))
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())
第三方模块的安装及使用

如下演示安装第三方模块schedule

import time
import schedule
def job():
print('job executed')
schedule.every(3).seconds.do(job)
while True:
schedule.run_pending()
time.sleep(1)

总结

1767

被折叠的 条评论
为什么被折叠?



