Def function defined functions must parameter variable length parameters: def add (* args) add (1,2,3,4,5) no tuple named parameters def add (** args) print_info ( 'wenson', 30, 'male ') of the key into the dictionary to store the location of the variable-length parameters: * args on the left, ** kwargs parameters on the right values passed with no name tuple, have spread the name of the dictionary, there are variable length default parameters discharge left, did not put the right return value: # return value # DEF F (): # Print ( 'OK') # return 10 # effect: 1 end function, 2 returns a image # a = f () Print # (a) # DEF the Add (* args): point # Note: 1. If the function does not return, a default return None # 2 # return If multiple objects, multiple objects will python then packaged into a million. group returned to # the Sum = 0 # for I in args: # the Sum + = I # Print (the Sum) # return the Sum # A = the Add (1,2,3,4,5) # Print (A) # Scope: built_in built ---- global scope of the global scope --- enclosin nested scopes --local local scope # only modules, classes, and functions in order to introduce a new scope # inner scope to modify external when the value of variable scope, global variables to use the global keyword, nested scopes variables to use nonlocal keyword #nonlocal is new python3 keyword # COUNT = 10 # DEF Outer (): # Print (COUNT) # Outer () # recursive functions: 1. call itself functions, 2 have an end condition 3 but all recursion can write cycle can be achieved, is not efficient # DEF F (n-): # sum_f = 1 # for I Range in (. 1, n-): # = sum_f sum_f * I # return sum_f # Print (F (. 7)) # # DEF FAC (n-): # n-IF. 1 ==: # return. 1 # n-return * FAC (n- -1) # Print (FAC (. 9)) # # Fibonacci series # DEF FIBO (n-): # n-IF or n-== == 0. 1: # Return n- # DEF ADD1 (X, Y): # Return FIBO (n--. 1) + FIBO (n--2) # Print (FIBO (. 9)) # important built-in function # filter # worrying too much # str = [ 'a', 'b', 'c', 'd '] # DEF fun1 (S): ! # IF S =' a ': # misplaced' a ' # return S # RET = filter (fun1, STR) # Print (RET) # <filter Object AT 0x0000000001051C88> RET is a iterator object # Print (List (RET)) # [ 'B', 'C', 'D'] # Map # modifier # str = [ 'a', 'b', 'c', 'd'] DEF fun2 # (S): # S + return "Alvin" # [ 'aalvin', 'balvin', 'Calvin', 'Dalvin'] # RET = Map (fun2,STR) # Print (RET) # map objectr iterator # Print (List (RET)) # # stacker #reduce # Import from the reduce functools # # return X + Y # Print (reduce (add1, range (1,101))) # The result is a value #lambda anonymous function from the reduce functools Import Print (the reduce (the lambda A, b: A b *, the Range (1, 6)))
Def function-defined functions
Guess you like
Origin blog.csdn.net/wenson0705/article/details/89076512
Ranking