3 python- function (global variables and local variables)

3 python- function (global variables and local variables)

Global variables and local variables

School = " Goy EDU "           global variables uppermost defined
 DEF   Change_Name (name):
     Global   School global variable processing, local functions in a Global 
    School = " kezi EDU "        local variable effect inside this function
     Print ( " before Change " , name, School)      
    name = " HK " 
    Age = 23 is
     Print ( " After Change " , name, School) 

name = " HK " 
Change_Name (name) 
Print (name)
Print (School) 

hit mark Results 
Before Change Hk Kezi Edu 
After Change HK Kezi Edu 
Hk 
Kezi Edu

Note: Do not modify the function of the following ways

def change_name():
    global name
    name="kezi"


change_name()
print(name)

Do not define a global change management in the following manner. Do not define global variables within the function. If global variables out there, so you can use.

name=["kezi","jaja","huahua"]
def change_name():
     print("before change",name)
     name[0]="科子"
     print ("after change",name,)

change_name()
print(name)

打印结果
before change ['kezi', 'jaja', 'huahua']
After change [' Section child ' , ' jaja ' , ' huahua ' ] 
[ ' Section child ' , ' jaja ' , ' huahua ' ]

Note: In addition to characters, numeric variables, others such as lists, dictionaries, collections, local variables, global variables can be modified

 

Guess you like

Origin www.cnblogs.com/kezi/p/11968576.html