15.python variables, recursion

 Original: https://www.cnblogs.com/linhaifeng/articles/6113086.html#_label6

# If the content of the function have global keyword 
#    - have declared a local variable 
        # NAME = [ "product manager", "Liao wave wet"] 
        # DEF qupengfei (): 
        #      global NAME 
        #      NAME = "own" 
        #      Print ( 'I engage ', NAME) 
        # qupengfei () 
#    - error example 
        # NAME = [ "product manager", "Liao wave wet"] 
        # DEF qupengfei (): 
        #      NAME = "own" 
        #      , Ltd. Free Join NAME 
        #      Print (' I want to out ', NAME) 
        # qupengfei () 
#    - no declaration of local variables 
        # NAME = [ "product manager", "wet Liao wave"] 
        # DEF qupengfei ():
        #     global NAME
        #     NAME = [ "Mao"] 
        #      NAME.append ( 'XXOO') 
        #      Print ( 'I wants', NAME) 
        # qupengfei () 

# ####### global variable variable name in uppercase 
# #### ### local variable variable name lowercase 


# priority read local variables, global variables can be read, can not be reassigned nAME = "fff" global variables, 
#      but for the variable type, you can be the internal operating elements 
# If the function there are global keyword, the variable is essentially a global variable that can be read assignable NAME = "fff"

Function can be nested between

= NAME ' sea breeze ' 

DEF huangwei (): 
    name = " Michael " 
    Print (name)
     DEF Liuyang (): 
        name = " Liu Yang " 
        Print (name)
         DEF nulige (): 
            name = ' main stock index spent ' 
            Print (name)
         Print (name) 
        nulige () 
    Liuyang () 
    Print (name) 

huangwei () 
# Michael 
# Liu Yang 
# Liu Yang 
# main stock index spent
# Michael

 

 

 

DEF weihou (): 
    name = " Raymond " 
    DEF weiweihou (): 
        nonlocal name    # nonlocal, specify the primary variable, if not continue up until you find the 
        name = " cool " 

    weiweihou () 
    Print (name) 

Print ( name) 
weihou () 
Print (name)
 # just Mother 
# cool 
# just Mother

 

DEF foo ():
     Print ( ' from foo ' ) 
    bar () 

foo () # given, undefined 

DEF bar ():
     Print ( ' from bar ' )

Recursion:

def calc(n):
    print(n)
    if int(n/2) ==0:
        return n
    return calc(int(n/2))
 
calc(10)
 
输出:
10
5
2
1

 

import time

person_list=['alex','wupeiqi','linhaifeng','zsc']
def ask_way(person_list):
    print('-'*60)
    if len(person_list) == 0:
        return '根本没人知道'
    person=person_list.pop(0)
    if person == 'linhaifeng':
        return '% s said: I know, old boy in Shahe The German commercial buildings, the subway is ' % the Person 

    Print ( ' hi handsome [% s], ask where ' % the Person)
     Print ( ' % s replied: I I do not know, but read you identify what a pig, you wait, I'll ask ... S% ' % (the Person, person_list)) 
    the time.sleep ( 1 ) 
    RES = ask_way (person_list)
     return RES 
RES = ask_way ( person_list)
 Print (RES)

 

Recursive nature:

1. There must be a definite end condition

2. Each entry recursive deeper, the scale of the problem should be reduced compared to the previous recursive

3. recursive efficiency is not high, too much will cause the level of recursion stack overflow (in the computer, the function call is achieved through the stack (Stack) This data structure, each time a function call into the stack the stack frame is increased by one whenever the function returns, the stack will cut a layer stack frame. Since the size of the stack is not infinite, so the number of recursive calls too much will result in a stack overflow)

Stack literacy http://www.cnblogs.com/lln7777/archive/2012/03/14/2396164.html 

Tail recursion optimization: http: //egon09.blog.51cto.com/9161406/1842475

Guess you like

Origin www.cnblogs.com/raitorei/p/11682188.html