Python functions
1. def defines function keywords;
2. Function name, call the function by function name later;
3. function declaration, not automatic execution;
4. Parameters of the function;
5. Return value of the function;
return value:
1. The return value is not explicitly specified and returns none;
2. The return value can be assigned to a variable;
3. The return value can be a variable or a data structure.
Number of arguments:
1. Formal parameters
2. Actual parameters
3. Function can define multiple parameters
Default parameters:
1. Do not pass, the default value is used;
2. Pass the parameter, overriding the default value.
Dynamic Parameters:
1. Receive multiple parameters, when transmitting the sequence;
2. Internal automatic construction of the Yuan zu;
3. Sequence, *, avoid the internal automatic construction of the progenitor.
Define the format of the function:
def function name ():
function content; function content
function content; function content
function name () # Execute function
# 1. Implement the function Len function to take string length
a= "Hellomyteacher"
print Len (a)
# 2. Implement the cut split function for a string
a= "Student"
B=a.split ("U")
Print B
C:\Python27\python.exe "e:/python-file/learn-python.py"
[' st ', ' Dent ']
Process finished with exit code 0
# Custom Functions
def A ():
print "Hello";p rint 777
A ()
C:\Python27\python.exe "e:/python-file/learn-python.py"
Hello
777
Process finished with exit code 0
function is implemented by Reuturn to let function return worthwhile function
The return value of a function has three cases:
1. A case of a return value
def test ():
i=7
return I
Print test ()
2. A variable stores multiple return value-assigned cases
def test2 (i,j):
K=i*j
return (i,j,k)
X=test2 (4,5)
Print X
C:\Python27\python.exe "e:/python-file/learn-python.py"
(4, 5, 20)
Process finished with exit code 0
3. Scenarios where multiple variables store multiple return values
def test3 (i,j):
K=i*j
return (i,j,k)
X,y,z=test3 (4,5)
Print X
Print Y
Print Z
C:\Python27\python.exe "e:/python-file/learn-python.py"
4
5
20
Process finished with exit code 0
Default parameters:
Def show (args,arg1= ' a '):
Print (ARGS,ARG1)
Print (ARGS,ARG1)
Show (12,arg1= ' 1 ')
Show (12)
12 1
12 1
A
A
Dynamic Parameters:
1)
Def show (*args,**kwargs):
Print (Args,type (args))
Print (Kwargs,type (Kwargs))
Show (12,34,a=1,b=2)
<class ' tuple ' >
{' A ': 1, ' B ': 2} <class ' Dict ' >
2)
>>> def Show (*args,**kwargs):
.. print (Args,type (args))
.. print (Kwargs,type (Kwargs))
...
>>> i = [11,22,33]
>>> v = {' K2 ': 1, ' K1 ': 2}
>>> Show (i,v)
([One, all,], {' K2 ': 1, ' K1 ': 2}) <class ' tuple ' >
{} <class ' dict ' >
>>> Show (*i,**v)
(One, one, one) <class ' tuple ' >
{' K2 ': 1, ' K1 ': 2} <class ' Dict ' >
3)
>>> test = ' name: {0}, pwd: {1} '
>>> test.format (' Lihongye ', ' pwd1 ')
' Name:lihongye, Pwd:pwd1 '
>>> test = ' name: {x}, pwd: {y} '
>>> Test.format (x= ' Lihongye ', y= ' pwd1 ')
' Name:lihongye, Pwd:pwd1 '
>>> test_dic = {' x ': ' Lihongye ', ' y ': ' Pwd1 '}
>>> Test.format (**test_dic)
This article is from the "a myriad of simple pleasures of technology to enjoy" blog, please be sure to keep this source http://51enjoy.blog.51cto.com/8393791/1736057
Python path (ii) function