Tag:python function
1. A simple non-parametric function #!/usr/bin/evn python#filename: function1.pydef sayhello (): print ' hello world! ' SayHello () 2. function parameter #!/usr/bin/evn pyhon # filename: fun_param.pydef printmax (A, B): if a > b: print a, ' Is maxinum ' else: print b, ' Is maxinum ' Printmax (3, 4) X = 5y = 7printmax (x, y) 3. Global Parameters #!/ Usr/bin/env python#filename : fun_global.pydef func (): global x print ' X is ', x x= 2 print ' changed local x to ', xx = 50func () print ' Value of x is ', x4. Default parameter #!/usr/bin/env python#filename: fun_default.pydef saY (message, times = 1): print message * timessay (' Hello ') Say (' World ', 5) 5. Key parameter #!/usr/bin/env python#filename: fun_key.pydef func (a, b=5, c=10): print ' a is ', a, ' and b is ', b, ' and c is ', cfunc (3, 7) func (25, c=24) func (c=50, a=100) 6.return statement #!/usr/bin/env Python# filename: fun_return.pydef maximum (x, y): if x > y: return x else: Use of return yprint maximum (2, 3) 7.DocStrings, Document string Note when calling with the. __doc__#!/usr/bin/env python# filename: fun_doc.pydef 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__
This article is from the "Notes" blog, so be sure to keep this source http://sunflower2.blog.51cto.com/8837503/1546258
Use of Python----functions