python study notes, video day14- function

function

  advantage

  1. Code reuse
  2. Consistent, easy to maintain
  3. Scalability

  Function Arguments

    Parameter: allocate memory when called to free the memory at the end of call

    Argument: take up memory

    One to one positional parameters, keyword parameters

  to sum up:

    Returns the value = 0, None

    Returns the number of the function name = value, the function returns a return value

    Other values ​​= Returns, return other

  Process : that is, the function does not return a value

  Supplementary : two functions the same name, calls the new function

  Function encounters end return immediately

  

def test(x):
    "The function definitions "
    y=2*x+1
    return y
a=test(3)
print(a)
# 结果
# 7

  

Test DEF (X): 
    "Definitions of The function" 
    Y = 2 * X +. 1 
    return Y Print (Test) 
# results, print out the memory address # <function test at 0x008D07C8>


# No return value is None 
DEF Test01 (): 
    MSG = " Hello " 
    Print (MSG) 

DEF Test02 (): 
    MSG = " of hello2 " 
    Print (MSG)
     return MSG 
T1 = Test01 () 
T2 = Test02 ()
 Print (T1)
 Print (T2)
# 结果
# hello
# hello2
# None
# hello2
def test01():
    msg="hello"
    print(msg)

def test02():
    msg="hello2"
    print(msg)
    return msg
def test03():
    msg="hello3"
    print(msg)
    return 1,2,43,{"name":"alex"}
t1=test01()
t2=test02()
t3=test03()
print(t1)
print(t2)
print(t3)

# 结果
# hello
# hello2
# hello3
# None
# hello2
# (1, 2, 43, {'name': 'alex'})

 Function Arguments

# Position parameter correspondence indispensable, nor a multiple 
DEF Test (X, Y, Z):
     Print (X)
     Print (Y)
     Print (Z) 
Test ( 1,3,2 )
 # Results 
# 1 
# 3 
# 2 
# keyword parameter correspondence need not indispensable, nor a multiple 
DEF Test (X, Y, Z):
     Print (X)
     Print (Y)
     Print (Z) 
Test (X =. 1, 3 = Z, Y = 2 )
 # results 
# 1 
# 2 
# 3 
# mix position parameter must be left key parameter, a position parameter must correspond 
defTest (X, Y, Z):
     Print (X)
     Print (Y)
     Print (Z) 
Test ( l, 3, Z = 2 )
 # Results 
# 1 
# 3 
# 2
# Default parameters, advance assignment of 
DEF handle (the X-, of the type = None):
     Print (the X-)
     Print (of the type) 
handle ( 1 )
 # result 
# 1 
# None 
DEF handle (the X-, of the type = None):
     Print (the X-)
     Print (type) 
handle ( " Hello " , type = " Hi " )
 # results 
# Hello 
# Hi 
# position parameter 
DEF handle (X, type = None):
     Print (X)
    print(type)
handle("hello","hi")
# 结果
# hello
# hi

Parameters: * List, ** Dictionary

# Parameter group: ** dictionary, list * 
DEF Test (X, * args):
     Print (X)
     Print (args)
     Print (args [1 ]) 
Test ( 1,22,34,5 )
 # Results 
# 1 
# ( 22, 34, 5) 
# 34
# Does not pass an empty 
DEF the Test (the X-, * args):
Print (the X-)
Print (args)
the Test (1)
# result
# 1
# ()
 
# If a dictionary, as a tuple in the 
DEF Test (X, * args):
     Print (X)
     Print (args) 
Test ( . 1, { " name " : 22 is })
 # Results 
# . 1 
# ({ 'name ': 22}) 
# if a list, as a tuple in the 
DEF Test (X, * args):
     Print (X)
     Print (args)
     Print (args [0] [0]) 
Test ( . 1, [ " X " , " Y " , " Z " ])
 #Results 
# . 1 
# ([ 'X', 'Y', 'Z'],) 
# X 


#
* [ "X", "Y", "Z"], the list traversal again sequentially assigned; [ "x", "y", "z"] represents a list as a whole had to pass first element
def test(x,*args):
    print('x')
print(args)

test(1,*["x","y","z"])
# 结果
# ('x', 'y', 'z')
 

 

# A two parameter values can not pass 
DEF Test (X, ** kwargs):
     Print (X)
     Print (kwargs) 
Test ( . 1, Y = 2, Z =. 3 )
 # Results 
# . 1 
# { 'Y': 2, 'Z':}. 3 

DEF Test (X, * args, ** kwargs):
     Print (X)
     Print (args)
     Print (kwargs) 
Test ( 1, Y = 2, Z =. 3 )
 # results 
# 1 
# () 
# { 'Y': 2, 'Z':}. 3 
DEF Test (X, * args, ** kwargs):
     Print (X)
     Print(args)
     Print (kwargs) 
Test ( 1,2,5,76,7, Y = 2, Z =. 3 )
 # Results 
# . 1 
# (2,. 5, 76,. 7) 
# { 'Y': 2, ' z ': 3}

 

 

  

Guess you like

Origin www.cnblogs.com/ppll/p/11538046.html