Functions and parameters of Day11.python

Source: Internet
Author: User

Functional programming The most important thing is to enhance the reusability and readability of code

def function name (parameter):2      3     ... 4      function body 5     ...

The definition of a function has the following main points:

    • def: A keyword that represents a function
    • Function name: the names of functions, which are called later by function name
    • Function Body: A series of logical calculations in a function, such as sending a message, calculating the maximum number in [11,22,38,888,2], etc...
    • Parameters: Providing data for the function body
    • Return value: Once the function has finished executing, it can return data to the caller.

In the above points, it is more important to have parameters and return values:

1. Return value

A function is a function block that executes successfully or is required to inform the caller by the return value.

 def   send SMS (): Code to send SMS ...  if   sent successfully:  return   True  else  :  return   False  while   #      each time a send SMS function is executed, the return value is automatically assigned to result  #    result  = send SMS ()  if  result == False: Record log, SMS send failed ...  
View Code

2. Parameters

There are three different parameters to the function:

    • General parameters
    • Default parameters
    • Dynamic parameters

General parameters:

There is no limit to the number and data type of the normal parameter passing, and you can pass strings, numbers, lists, and dictionaries. Also do not limit the number, it should be noted that the function requires a number of parameters, the call should be in accordance with its definition of the order and data type passed in the past.

1defargtest (name,age,fruit_lst,hobby_dic):2Print 'hello,me name is%s,i\ ' m%d year\ ' s old'%(name,age)3Print 'My favorite fruits is:%s'%'.'. Join (FRUIT_LST)4Print 'My Hobbies:'5 forHobbyinchHobby_dic:6Print '\t%s'%Hobby_dic[hobby]7 8 LST = ['Apple','Banana','Watermelon'] 9 dic = {'Hobby One':'Teaism','Hobby':'Sing'}Ten Argtest ('Eva_j', 18, Lst,dic) output content: Hello,me name isEva_j,i'm Year's oldmy favorite fruits are:apple.banana.watermelonMy hobbies:teaism sing
View Code

Default parameters:
The default parameter is to add a default value to the parameter, but when we call the function, if we pass this parameter, we use the value passed by us, and if you do not pass the default value, the default argument can have more than one, but it must be placed after the normal parameter. When we have more than one default parameter, we can use the parameter name to specify one of the parameters when calling the function, so that the parameter can be passed to the called function accurately.

1defArgvtest (argv1,argv2 ='AAA', Argv3 ='BBB'):2Print 'ARGV1:', Argv13Print 'Argv2:', Argv24Print 'Argv3:', Argv35 6 Argvtest ('A1', Argv3 ='A2') content of output: Argv1:a1argv2:aaaargv3:a2
View Code

Dynamic Parameters:
def func (*args) accepts multiple positional parameters, internally automatically constructs tuples, adds * before sequence, avoids internal tectonic tuples
def func (**kwargs) receives multiple keyword arguments, internally automatically constructs a dictionary, adds * * before sequence, and passes the dictionary directly
def func (*args,**kwargs): Accepts multiple parameters, which can be used to construct tuples automatically and to construct dictionaries automatically.

Automatically constructs a tuple from left to right, automatically constructs a dictionary and a combination of the first two parameters to pass the way:

Functions and parameters of Day11.python

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.