(iii) General form and parameters of 3-3 python

Source: Internet
Author: User

Functions of Python
A, the general form of the function
Define a function that needs to meet the following rules
function code block starts with DEF keyword, followed by function identifier name and parentheses ()
Any incoming parameters and arguments must be placed in the middle of the parentheses, and the parentheses can be used to define the parameters
The first line of the function statement can optionally use the document string, which holds the description of the function.
The function contents begin with a colon and are indented.
return [expression] End Function, optionally return a value to the caller, return without an expression is equivalent to returning none
A simple example of a function

def sum (x, y    ): Print ('x = {0}'. Format (x))     Print ('y = {0}'. Format (y))     # print (' x =%d '%x)    # print (' y =%d '%y)    return x += ten= sum (A, b)print(c)

Operation Result:

x = ten= 2030

Note:
1, Def is a function of the key word, the format is this
2, Def followed by the function name, this can be customized
3, the parentheses inside the function is passed the parameters, which can be used in the function
4, sum (A, B) at the time of execution, the "a" and "C" is passed to the SUM function
5, return is the result when we call the function, the call function returns two number of the C=a+b

b, various parameter types of the function

You will often see how func (*args,**kwargs) functions are defined
Example 1:
1. Set a default value for variable b
If a value of B is specified when the argument is passed in, then there is a default value when B has no value

def Funca (a,b=0):    print("" + str (a))      Print("" + str (b)) Funca (1)

Operation Result:

A = 1= 2

2, the parameter is tuple (tuple)

def FUNCB (a,b,*C):    print(a)    print(b)      Print("length of C is: {0}". Format (len (c)))    Print (c) FUNCB (1,2,3,4,5,6) # or # test = ("Hello", "World") # FUNCB (1,2,*test)

Operation Result:

is: 4(3, 4, 5, 6) 

3. The parameter is a dictionary

defFUNCC (a,**b):Print(a)Print(b) forXinchB:Print(x +":"+str (b[x]))Print("*"*10)     forKvinchB.iteritems ():Print(k +":"+v) FUNCC (100,x="Hello", y="Cnblogs")Print("*"*20) args= {"1":"a","2":"b"}FUNCC (A= 100,**args)

Operation Result:

100{'y':'Cnblogs','x':'Hello'}y:cnblogsx:hello**********Y:cnblogsx:hello100{'1':'a','2':'b'}1: A2: b1: A1: A

(iii) General form and parameters of 3-3 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.