python basis - function 1

# First, why use the function 
#    1, you can make the organization structure of the code clear and readable 
#    2 encountered repeated problems can directly call the function 
#    3, extensions can be modified directly, without having to have carried out at each modify, 
# Second, the function of the mass 
# function programmer's equivalent housewife in meters, Lanxiang of excavators 
# students in the books. 
# Third, the classification function 
# 1, built-in functions, python interpreter has given us a lot better defined function to help us to 
#    to develop, we can be directly used, without defining, for example: len, man, min, sum 
# 2, custom functions, it is clear that python function built-in functions are limited, or else we would not have learned, 
#    direct call it, we need to customize function according to their own needs, 
# IV. Defined Functions 
DEF MAX_NUM (num1, num2):
     IF num1> num2:
         return num1
     the else : returnnum2
 # function name: max_num: should see the name to know Italy, 
# parameters: num1, num2: when calling should be based on whether there are parameters to decide whether to pass argument 
# Returns: do not write default return empty 
# needs to be defined, in call 
# NUM = sum_num (3, 6) 
# DEF sum_num (num1, num2): 
#      return num1 + num2 
# on top of this wording is wrong. 
# Function definition, you can just detect syntax does not execute code 
# That syntax error in the function definition phase will be detected, 
# logic error code only when executed will know 
# Five calls the function 
# function name brackets, passing parameters, if there is a return value 

# Sixth, the function parameter 
# parameter that is the variable name, an argument that is a variable value, the function call, bind the values to the variable name, 
# at the end of the function call, lift binding 
DEF modify_num (num1, num2): 
    num1 =. 5
    num3 =. 6 
n1, n2 = 3,5 
modify_num (n1, n2) 
Print (n1, n2)
 # parameter is num1, num2, 3, 5 argument 
# At this time the value of n1, n2 or does not become, 
# several function parameters: 
# 1, positional parameters: from left to right in accordance with the parameters defined sequence 
#    position parameter: Required parameter argument positions: a position according to the shape parameters by value 
# 2, key parameters: in accordance with key = value of the argument defined in the form 
#    not according to the position to pass parameter values 
#    (1), key parameters in the right argument must position 
#    (2), to pass a parameter value can not be repeated with 
# 3, the default parameters: in the definition is already assigned values 
# can be passed by value, the value may not be worn, 
#    1, while only assigned once defined, 
#    2, default parameters should be defined at a position to the right shape parameter 
#    3, generally immutable type 
# 4, variable-length parameters: 
#Argument is not worth the number of fixed, 
# argument is defined by location and key in two forms, args *, ** kwargs. 
DEF foo (X, Y, * args):
     Print (X, Y)
     Print (args ) 
foo ( . 1, 2,. 3,. 4,. 5 ) 

DEF foo (X, Y, * args):
     Print (X, Y)
     Print (args) 
foo ( . 1, 2, * [. 3,. 4,. 5 ]) 

DEF foo (X, Y, Z):
     Print (X, Y, Z) 
foo ( * [. 1, 2,. 3 ]) 


DEF foo (X, Y, ** kwargs):
     Print (X, Y)
     Print (kwargs) 
foo ( . 1, Y = 2, A =. 1, B = 2, C =. 3 )

def foo(x, y, **kwargs):
    print(x, y)
    print(kwargs)
foo(1, y=2, **{'a': 1, 'b': 2, 'c': 3})

def foo(x, y, z):
    print(x, y, z)
foo(**{'z': 1, 'x': 2, 'y': 3})

 

Guess you like

Origin www.cnblogs.com/cong12586/p/11353668.html