Python local and global variables

local and global variables

# coding:utf-8

local and global variables

ahead = 'HelloWorld' # global variable
showList = [] # global variable

def printAhead():
print(ahead)

def printOther():
city = 'beijing' #city is a local variable
print(city + ahead)

def printList():
global showList #global means to refer to global variables, no way, if you don't write it, showList will become a local variable
showList.append(1)
showList.append(2)
print(showList)

printAhead()
printOther()
printList()

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325344635&siteId=291194637