Python Basic---functions

Source: Internet
Author: User

Function

Once defined, it can be called wherever it is needed
return value
3 cases of Return value
No return value--return None
Do not write return
Write only return: End a function's continuation
Return None --infrequently used
Returns a value of 1
Can return any data type
You can receive it as soon as you return.
If there are multiple return in a program, only the first one is executed
Return multiple values
Receive with multiple variables: How many variables are received with the number of return values
Receive with a variable: a tuple is obtained

Return End Function
1 #write only return: Ends a function's continuation, does not output print (' = ' *10)2 #and break is the difference, break is jump out of the loop, return is the End Function3 deffunc ():4L = ['haha','Oh, yes.']5      forIinchL:6         Print(i)7         ifi=='Oh, yes.':8             return9     Print('='*10)TenRET =func () One Print(ret)

return value

1 deffunc ():2     return{'k':'v'}3 Print(func ())4 5 defFunc2 ():6     returnThe#return7R1,R2,R3 =Func2 ()8 Print(R1,R2,R3)#Three accepted9 TenR= Func2 ()#a variable accepts One Print(r)#Output Tuple
Parameters
No Parameters  
no content is written in parentheses when defining functions and calling functions
has one parameter  
Pass what is what
has multiple parameters  
Position Parameters

at the angle of the argument:
pass the parameter by location
follow the key word parameters
mixed use can: but must first follow the location of the parameter, and then follow the key to pass the parameters
cannot pass multiple values to the same variable

standing on the angle of the formal parameter:
positional parameters: Must pass, and have several parameters to pass several values
Default parameters: You can not pass, if not pass is the default parameters, if the transmission of the


only when the function is called:
Pass by location: The value of the direct write parameter
by keyword: keyword = value

when defining a function:
positional parameters: defining parameters directly
default parameter, keyword argument: parameter name = ' Default value '
Dynamic Parameters: can accept any number of parameters
before the parameter name plus *, used to parameter name args,
before parameter name plus * *, custom parameter name Kwargs
Order: Positional parameters, *args, default parameters, **kwargs
Parameters
1 defMy_len (s):#A custom function requires only 0 parameters, receive parameters, formal parameters , formal parameter2i =03      forKinchS:4i + = 15     returnI#return value6 7ret = My_len ('Golden boss, little nurse .')#Pass Parameters: arguments, actual parameters, arguments8ret = My_len ([1,2,3,4,5])#passing Parameters: Parameter transfer9 Print(ret)
A parameter
1 defF2 (L1):2 F1 (L1)3      forIinchL1:4         Print(i)5 6 defF1 (L1):7      forIinchL1:8         Print(i)9 TenF2 ([1,2,3,4])
Multi-parameter
1 def classmate (name,sex): 2     Print ('%s:%s'% (name,sex))
Default parameters
1Classmate'two elder brother','male')2Classmate (sex='male', name ='two elder brother')3 4 defClassmate (name,sex='male'):5     Print('%s:%s'%(name,sex))6 7Classmate'two elder brother')8Classmate'Valter', sex='female')
Dynamic Parameters:  can accept any number of parameters
*args   : Received by the value of the location parameter, organized into a tuple **kwargs: accepted by the keyword to the value of the parameter, organized into a dictionary
Dynamic parameter  *args    #可以接受任意多个参数
1 defSUM (*args):#before parameter name plus *, custom parameter name args2n =03      forIinchargs:4n+=I5     returnN6 7 Print(SUM ())8 Print(SUM ())9 Print(SUM (1,2,3,4))
Dynamic parameter  **kwargs    #可以接受任意多个参数
1 def func (**kwargs):  # before parameter name plus * *, custom parameter name Kwargs2     print  (Kwargs) 3 4 func (a = 1,b = 2,c =3)5 func (a = 1,b = 2)6 func (a = 1)
There are two types of dynamic parameters: one can accept any parameter
1  *args: Received by the value of the location parameter, organized into a tuple  2  **kwargs: Accepted by the value of the keyword argument, organized into a dictionary  3  args must be before Kwargs  4  5   def  func (*args,default = 1,**kwargs):  6  print   (Args,kwargs)  7  8  func (1,2,3,4,5,default=2,a =  " aaaa   ", B = "  bbbb   ,) 
Another method of parameter transfer for dynamic parameters
1 defFunc (*args):#standing at the angle of the parameter, adding * to the variable is the combination of all the transmitted values. 2     Print(args)3 4Func (1,2,3,4,5)5L = [1,2,3,4,5]6Func (*l)#at the angle of the argument, add * to a sequence, which breaks the sequence in order7 8 defFunc (* *Kwargs):9     Print(Kwargs)Ten  OneFunc (a=1,b=2) AD = {'a': 1,'b': 2}#define a dictionary D -Func (**d)
Comment on the function
1 def func (): 2     " " 3     what function does this function achieve 4     parameter 1:5     parameter 2:6     : Return: Is the length of the string or list 7     " " 8     Pass

Python Basic---functions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.