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__
本文出自 “笔记” 博客,请务必保留此出处http://sunflower2.blog.51cto.com/8837503/1546258
原文地址:http://sunflower2.blog.51cto.com/8837503/1546258