发布时间:2019-06-30 15:17:48编辑:auto阅读(1698)
1.一个简单的无参函数
#!/usr/bin/evn python
#filename: function1.py
def sayHello():
    print 'Hello World!'
sayHello()
2.函数传参
#!/usr/bin/evn pyhon 
# Filename: fun_param.py
def printMax(a, b):
    if a > b:
        print a, 'is maxinum'
    else:
        print b, 'is maxinum'
printMax(3, 4)
x = 5
y = 7
printMax(x, y)
3.全局参数
#!/usr/bin/env python
#filename : fun_global.py
def func():
    global x
    print 'x is', x
    x= 2
    print 'Changed local x to ', x
x = 50
func()
print 'Value of x is', x
4.默认参数
#!/usr/bin/env python
#filename: fun_default.py
def say(message, times = 1):
    print message * times
say('Hello')
say('World', 5)
5.关键参数
#!/usr/bin/env python
#filename: fun_key.py
def func(a, b=5, c=10):
    print 'a is', a, 'and b is', b, 'and c is', c
func(3, 7)
func(25, c=24)
func(c=50, a=100)
6.return语句
#!/usr/bin/env python
# filename: fun_return.py
def maximum(x, y):
    if x > y:
        return x
    else:
        return y
print maximum(2, 3)
7.DocStrings的使用,文档字符串
注意调用的时候用.__doc__
#!/usr/bin/env python
# filename: fun_doc.py
def printMax(x, y):
    '''Prints the maximum of two numbers.
    The two values must be integers.'''
    x = int(x)
    y = int(y)
    if x > y:
        print x, 'is maximum'
    else:
        print y, 'is maximun'
printMax(3, 5)
print printMax.__doc__
上一篇: Python操作Mysql数据库的实现
下一篇: Python pyperclip模块的使
 51191
 50602
 41228
 38046
 32510
 29416
 28277
 23129
 23091
 21424
 1476°
 2194°
 1818°
 1748°
 2059°
 1810°
 2493°
 4183°
 4046°
 2889°