python study notes, video day15- global variables and local variables, rheumatism theory, recursive function

Recalling the last lesson

1. Function  

   In mathematical function: y = 2 * x + 1

   python parameters:

    def test(x,y,type=None):

      "Notes"

      Block

      return res

    Function name represents the memory address references, to add parentheses when calling

2. Parameters

 

  --- total memory parameter when you call, do not release memory when calling

Arguments - total memory

One to one positional parameters

Keyword arguments

The default parameters

Positional parameters in keyword arguments left

3.

   def test(x,*args,**kwargs):

      "Notes"

      Block

      return res

4. Functions benefits:

  Reduce duplication of code

  Easy to maintain

  Scalable

Global variables and local variables

  • Global variables: There is no indentation, which entered into force in the global variable
  • Local variables: variables defined in the subroutine
  • Only functions have privacy function
  • Interpreter running from top to bottom, the compiler does not perform functions that encounter only

    Rules: global variable variable name in uppercase, lowercase variable names of local variables it

global global variables, variables on a nonlocal

= name " . Yangzi " # global variable 
DEF Change_Name (): 
    name = " jinmi " local variable # 
    Print ( " chang_name " , name) 
Change_Name () 
Print (name)
 # Results 
# chang_name jinmi 
# . Yangzi

 global

  If the content of the function of the non-global keyword

  Priority read local variables, no local variables can be read global variables, global variables can not be assigned to; may operate for a variable number of internal elements variables

  •   There declare local variables
name="yangzi"
def change_name():
    name="jinmi"
    print("chang_name",name)
change_name()
# 结果
# chang_name jinmi
  •   No declaration of local variables
name="yangzi"
def change_name():
    print("chang_name",name)
change_name()
# 结果
# chang_name yangzi
name=["yangzi","jingtian"]
def change_name():
    name.append("123")
    print("chang_name",name)
change_name()
# 结果
# chang_name yangzi 

  There are global keyword, the variable is essentially a global variable can be assigned reading 

name=["产品经理","qianyue"]
def change_name():
    global name
    name="jinmi"
    print("chang_name",name)
change_name()
print(name)
# 结果
# chang_name jinmi
# jinmi
NAME  ="海峰"
def name1():
    name="huang"
    print(name)
    def liuyang():
        name="liuyang"
        print(name)
        def mulige():
            name="huzhihua"
            print(name)
        print(name)
        mulige()
    liuyang()
    print(name)

name1()
# 结果
# huang
# liuyang
#Liuyang 
# huzhihua 
# huang

#global global variable 
name = " Yangzi " DEF weihou (): name = " chenzhuo " DEF weiweihou (): , Ltd. Free Join name name = " lengjin " weiweihou () Print (name) Print (name) weihou () Print (name) # results # Yangzi # chenzhuo # lengjin
# nonlocal上一级变量
name ="yangzi"
def weihou():
    name="chenzhuo"
    def weiweihou():
        nonlocal name
        name="lengjin"
    weiweihou()
    print(name)
print(name)
weihou()
print(name)

# 结果
# yangzi
# lengjin
# yangzi

 

Rheumatism theory

 No function watch 05 python s3 day15 rheumatoid Theory i.e. variable .ev4

Recursive function

Internal calls the function

 

 

 

Guess you like

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