Functions and parameters of Day3-python

Source: Internet
Author: User

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

1234 def函数名(参数):    ...    函数体    ...



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.

123456789101112131415161718 def 发送短信():         发送短信的代码...     if 发送成功:        return True    else:        return False  while True:         # 每次执行发送短信函数,都会将返回值自动赋值给result    # 之后,可以根据result来写日志,或重发等操作     result = 发送短信()    if result == False:        记录日志,短信发送失败...

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.

123456789101112131415 def argtest(name,age,fruit_lst,hobby_dic):     print ‘Hello,me name is %s,i\‘m %d year\‘s old‘%(name,age)     print ‘My favorite fruits are : %s‘%‘.‘.join(fruit_lst)     print ‘My hobbies:‘ 5for hobby in hobby_dic:     print ‘\t%s‘%hobby_dic[hobby]     lst = [‘www‘,‘biyao‘,‘com‘]     dic = {‘hobby one‘:‘read‘,‘hobby two‘:‘sport‘}     argtest(‘Bruce‘,18,lst,dic)输出内容:Hello,me name is Bruce,i‘m 18 year‘s oldMy favorite fruits are : www.biayo.comMy hobbies: read sport


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 parameter can have more than one, but it must be placed at the end of all parameters. 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.

12345678910 def argvtest(argv1,argv2 = ‘aaa‘,argv3 = ‘bbb‘):    print ‘argv1:‘,argv1    print ‘argv2:‘,argv2    print ‘argv3:‘,argv3    argvtest(‘a1‘,argv3 = ‘a2‘)输出的内容:argv1: a1argv2: aaaargv3: a2


?


Dynamic Parameters:
def func (*args) accepts multiple parameters, internally automatically constructs tuples, adds * before sequence, avoids internal tectonic tuples
def func (**kwargs) receives multiple parameters, internally automatically constructs the dictionary, adds * * Before the 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:


Source: http://www.cnblogs.com/Eva-J/p/4963028.html

From for notes (Wiz)

Functions and parameters of Day3-python

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.