First, create a function
>>> Import Math
>>> x=1
>>> y=math.sqrt
>>> callable (x) #显示False
>>> callable (y) #显示True
1, Def, used to create functions
>>> def hello (name)
>>> return ' Hello, ' +name+ '! '
>>> Print (Hello (' World '))
Show: Hello, World
2. Add description for function
>>> def Square (x):
>>> ' calculates the square of the number X. '
>>> return x *x
>>> square.__doc__
Display: ' Calculates the square of the number x. '
View functions via Help
>>> Help (Square)
Second, function parameters
1. Abstract function
>>> def init (data):
>>> data[' first ']={}
>>> data[' Middle ']={}
>>> data[' last ']={}
>>> storage={}
>>> Init (storage)
>>> Storage
Display: {' middle ': {}, ' last ': {}, ' first ': {}}
2. Collect parameters
>>> def print_params (X,y,z=3,*pospar,**keypar)
>>> print (x, y, z)
>>> Print (Pospar)
>>> Print (Keypar)
Display: 1 2 3 (5,6,7) {' foo ': 1, ' Bar ': 2}
Note: ' * ', a single asterisk is used to unite common parameters, ' * * ', two asterisks are used to handle keywords
3. Power Calculation Product
>>> Power (3,2)
Showing: 9
4, Interval, interval
>>> interval (10)
Display: [0,1,2,3,4,5,6,7,8,9]
>>> interval (1,5)
Display: [1,2,3,4]
5, when the parameter name and the global variable name, the global variable will be masked, not directly accessible, if you want to access, you need to use Globals
>>> def Combine (parameter):
>>> print (Parameter+globals () [' parameter '])
>>> parameter= ' Berry '
>>> combine (' shrub ')
Display: Shrubberry
The abstraction of Python's learning notes