Lecture 14: Modules in python & running in the form of main program & packages & common content modules & installation and use of third-party modules


1. Module

xxx.py is a module. The
module contains: functions, classes (class attributes, class methods, static methods, instance attributes, etc.), statements.

2. Custom modules

import  math #数学运算模块
print(id(math)) #2462428279568
print(type(math)) # <class 'module'>
print(math) #<module 'math' (built-in)>
print((math.pi)) #3.141592653589793
print(dir(math))
print(math.pow(2,3)) #2的3次方  8.0
print(math.ceil(9.001))#向上取整 10
print(math.floor(9.999))#向下取整 9

from math import pi
from  math import pow
print(pi)
print(pow(2,3))


'''
如何导入自定义模块
在xx.py文件单击右键选择Mark Directory as 中的 Sources Root
再 import xx.py
'''

3. Run in the form of the main program

def add(a,b):
    return  a+b

if __name__ == '__main__':
    print(add(10,20)) #只有当点击运行当下.py文件时,才做这个操作

4. Packages in Python


'''
python中的包
    包是一个分层次的目录结构,它将一组功能相近的模块组织在一起
    作用:
        代码规范
        避免模块名称冲突
    包与目录的区别
        包含__init__.py文件目录称为包
        目录里通常不包含__init__.py文件
python包含多个包,每个包又包含多个模块

导入带有包的模块注意
import 后面接包名或者模块名
from...import 后面接函数名、包、模块、变量
'''

#导入learn_python包
import  learn_pythoy.model as A #A作为别名
print(learn_pythoy.modeL.a)

5. Commonly used built-in modules

'''
sys #解释器与环境操作的标准库
time #实践相关的各种函数的标准库
os  操作系统服务功能的标准库
calendar 提供与日期相关的各种函数的标准库
urllib 读取来自网上服务器的数据标准库
json 使用JSON序列化和反序列化对象
re 在字符串中执行正则表达式匹配和替换
math 标准算数运算函数的标准库
decima 精确控制运算精度、有效数位和四舍五入操作的十进制运算
logging 灵活的记录事件、错误、警告和调试信息等日志信息的功能
'''

import  sys #解释器与环境操作的标准库
import urllib.request
import  time #实践相关的各种函数的标准库
print(sys.getsizeof(24)) #28 字节
print(sys.getsizeof(True)) #28
print(sys.getsizeof(False)) #24

print(time.time()) #1614173376.8433468
#print(time.localtime(time.time())

print(urllib.request.urlopen('http://www.baidu.com').read())

6. Installation and use of third-party modules

Installation: input in cmd: pip install schedule and
then input: python
last input: import schedule
program succeeds without error

import  schedule
import  time

def job():
    print('____哈哈')

schedule.every(3).seconds.do(job)

while True:
    schedule.run_pending()
    time.sleep(1)
    
#3秒执行一次,休息1秒。 可以在自动发送文

Guess you like

Origin blog.csdn.net/buxiangquaa/article/details/114041078