1.模块的概念:
python 模块(module),是一个python 文件,以.py 结尾,模块能定义函数,类和变量,模块也能包含可执行的代码。
2. 模块的导入:
<1> import 模块名的基本语法:
import 模块名·
模块名.功能名()
例子:导入时间的模块
# import 导入,time 模块名 import time print("are you ok?") time.sleep(5)# 睡眠5秒钟/ 暂停5秒 print("yes,I'm good")
<2>from 模块名 import 功能名的基本语法:
from 模块名 import 功能名
功能名()
例子:
from time import sleep print("what's you name?") sleep(3)# 暂停/睡眠3秒钟, print("my name is Anna")直接写sleep 不用重复模块名time
<3> from 模块名 import * 的基本语法: * 意思是导入所有的方法
from 模块名 import *
功能()
例子:
from time import * print("hello") sleep(3)# 睡眠/暂停3秒钟 print("hello")
<4> 模块定义别名语法:(别名就是给具体的模块或者是具体的功能完成改名的效果)
1.import 模块名 as 别名
案例:
import time as t print("hello") t.sleep(3) print("hello")
2.from 模块名 import 功能 as 别名
案例:
from time import sleep as t print("hello") t(3)# 把sleep 改名为t print("world")