python3 fifteen notes: python function

One: Learn

  • Functions Overview
  • Return value of the function parameters and
  • Transfer parameter values ​​are passed by reference, and
  • Keyword arguments
  • The default parameters
  • Variable-length parameters

 

II: Functions Overview

1. Essence: function is the function of the package

 

2. Benefits:

To simplify the structure of the code, the code multiplicity increases (the degree of repeated use)
To modify certain features, only need to modify the function corresponding to

 

3. function definition

Format:
DEF function name (parameter list):
    Statement
    return expression

Note: The final return expression, you can not write, equivalent to return None

 

For example:

def myPrint():
    print("tester is a good girl!")
    print("tester is a nice girl!")
    print("tester is a beautiful girl!")

 

4. Function Call

Format: function name (parameter list)

Note: The parameter list is a function of caller information passed to the function, if no parameters, the parentheses can not be omitted

The nature of the function call: the argument to a procedure parameter assignment

For example:

myPrint()

 

III: Function parameters and return values

1. No function reference no return value

For example:

# Define a non-reference no return value of the function
DEF the myPrint ():
    Print ( "Tester IS A Good Girl!")
    Print ( "Tester IS A Nice Girl!")
    Print ( "Tester IS A Beautiful Girl!")

# call
myPrint ()

 

2. Function Parameters - according to the order parameters must pass, must correspond to the number of

Parameter (formal parameters): defining a function parentheses variables, the variables essentially

Arguments (actual parameters): transfer function to the data of the function call, the value essentially

For example:

def myPrint(str,age):
    print(str,age)

#transfer

a = 18
myPrint("tester",a)

 

3. The return value of the function

mySum DEF (num1, num2):
    # return the results to the calling function
    # after executing a return statement, the function is over, the back of the return code statements do not perform
    return num1 + num2

# calls
print (mySum (3,2) )

 

IV: transfer parameter values ​​are passed by reference, and

1. The value passed: passed is immutable type (numeric, string, a tuple), without changing the original

For example:

def fun1(num):
    num = 10

temp = 20
fun1(temp)
print(temp)

 

2. passed by reference: the type of transmission is variable (lists, dictionaries, set), changing the original

For example:

def fun2 (list):
    list [0] = 100

and = [1,2,3,4]
fun2 (it)
print (it)

 

Five: keyword arguments

1. Keyword parameters: inconsistent parameters at the time sequence defined function call allows

the myPrint DEF (STR, Age):
    Print (STR, Age)

A = 18 is
# no keyword parameters, parameter passing must define the parameters of the same order
myPrint ( "tester", a)


# Using keyword arguments, but under normal circumstances or to maintain order and parameters defined in the same order
myPrint (age = 19, str = "tester")

 

Six: the default parameters

1. Default parameters: the function is called, if no parameter, the default parameters

Example 1:

def myPrint(str="tester",age=18):
    print(str,age)

myPrint()

 

Example 2:
# use the default parameters, preferably the default parameters into the final
DEF the myPrint (STR, Age = 18 is):
    Print (STR, Age)

the myPrint ( "Tester")

 

Seven: variable length parameters - can handle more than the defined parameters

1. tuple receiving data

A first variable length parameters written * args, * args as a tuple, the tuple of data on behalf of *

Example 1:

# Plus an asterisk (*) variable stores all variable argument unnamed, if no parameter is specified in the function call, it is an empty tuple
DEF FUNC (name, * args):
    Print (name) # prints the first Tester
    Print (type (args)) # is a tuple
    for I in args:
        Print (I) # prints subsequent iS a Good Girl

FUNC ( "Tester", "iS", "a", "Good" , "girl!")


# * Arr parameter is not given back
func ( "tester")

 

Example 2: Find a plurality of numbers and
DEF shared by mySum (NUM *):
    SUM = 0
    for I in NUM:
        SUM = I +
    return SUM
Print (shared by mySum (1,2,3,4))

 

2. Accept the dictionary key data types

The second variable length parameter wording ** kwargs, ** kwargs a dictionary type, representative of key data **

Example 1:

def func2(**kwargs):
    print(kwargs)
    print(type(kwargs))

func2(x=1,y=2,z=3)

 

func2 (1,2,3) # will complain

 

3. accepts any type of data

* args as a tuple type, * on behalf of tuple data
** kwargs a dictionary type, representative of key data **

For example:

func3 DEF (* args, ** kwargs):
    Print (args) # prints (. 1, ( 'A', 2), [. 1, 2], {. 1, 2})
    Print (kwargs) print {# ' x ': 1,' y ' : 2,' z ': 3}

func3(1,("a",2),[1,2],set([1,2]),x=1,y=2,z=3)

Guess you like

Origin www.cnblogs.com/miaomiaokaixin/p/11498298.html