Defining functions
Abstest.pydef my_abs (x): if x >= 0: return x else: return-x
Start the Python interpreter in the current directory of the file, and use the from Abstest import my_abs to import the My_abs () function.
To define a mutable parameter:
Def calc (*numbers): sum = 0for N in numbers:sum = sum + N*nreturn sum>>calc () 5>>calc () 0>>nums = [All ]>>calc (*nums) 14
Keyword parameters:
def person (name, age, **kw):p rint (' name: ', Name, ' Age: ', age, ' other: ', kw) >>person (' Mico ', ') Name:mico age:30 oth ER: {}>>person (' adm ', 45,city= ' Beijing ') name:adm age:45 Other: "City": ' Beijing '}>>extra = {' City ': ' B Eijing ', ' job ': ' Engineer '}>>person (' Jack ', 45,**extra) Name:jack age:45 Other: {' City ': ' Beijing ', ' job ': ' Engineer '}
Recursive functions:
def fact (n): if n = 1:return 1return n*fact (n-1)
Basic Python Tutorial--functions