Python入门的36个例子 之 18
例1:
# _018
# This is a module (if write Chinese in a module, there will be a error)
def func1():
print 'This is function 1.'
def func2():
print 'This is function 2.'
def func3():
print 'This is function 3.'
# 019
# 使用“import”语句调用模块:
import _018_Module
_018_Module.func1()
_018_Module.func2()
_018_Module.func3()
output:
This is function 1.
This is function 2.
This is function 3.
例2:
# _018
# This is a module (if write Chinese in a module, there will be a error)
def func1():
print 'This is function 1.'
def func2():
print 'This is function 2.'
def func3():
print 'This is function 3.'
# 020
# 使用“from ... import ...”语句调用模块
from _018_Module import func2
import _018_Module
_018_Module.func1()
func2()
_018_Module.func3()
output:
This is function 1.
This is function 2.
This is function 3.
例3:
# _018
# This is a module (if write Chinese in a module, there will be a error)
def func1():
print 'This is function 1.'
def func2():
print 'This is function 2.'
def func3():
print 'This is function 3.'
# 021
# 使用“from ... import *”语句调用模块
from _018_Module import *
func1()
func2()
func3()
output:
This is function 1.
This is function 2.
This is function 3.
相关文档:
# 004
# 利用三引号(''' or """)可以指示多行字符串
print '''line1
line2
line3'''
# 另外,你还可以在三引号中任意使用单引号和双引号
print ''' "What's up? ," he replied.'''
# 否则,你好使用转义符来实现同样的效果
# 还是使用三引号好,不然就破坏了视觉美了
print ' \"Wha ......
# 015
# 默认参数的本质是:
# 不论你是否提供给我这个参数,我都是要用它,
# 那么我就要想好在你不向我提供参数的时候我该使用什么。
# 或者,这样来理解:
# 有一些参数在一般情况下是约定俗成的,
# 但,在极少情况下会有一些很有个性的人会打破传统而按照自己的习惯来做事
def theFirstDayInAWeek(theDay = 'Sunda ......
# 017
def lifeIsAMirror():
string = raw_input()
if string == 'I love you!':
return 'I love you, too!'
elif string == 'Fuck you!':
return ''
else:
return
# end of def
string = lifeIsAMirror()
if len(string) == 0:
print 'You have nothing.'
else: ......