模块(module),简单说是一个python文件,可以这样创建
# this is the file "my_module.py"
# we're going to use it in another program
def c_to_f(celsius):
fahrenheit = celsius * 9.0 / 5 + 32
return fahrenfeit
嗯,就是这样。
如何使用呢,导入模块用关键字import,这样导入上面的新建模块
import my_module
写一个代码试试怎么使用上面的名为my_module.py模块
import my_module
celsius = float(raw_input("enter a temperature in Celsius: "))
fahrenheit = c_to_f(celsius)
print "That's ", fahrenheit, " degrees Fahrenheit"
要把模块文件放在一个python能找到的地方,试试看吧。
python提供了大量标准模块,先来看看time模块
import time
print "how",
time.sleep(2)
print "are",
time.sleep(2)
print "you",
time.sleep(2)
print "today?"
python的特点就是模块,除了标准模块外,还有很多实用的第三方模块,我们后面就会用一个图形模块pygame。
下一次,就来介绍这个。