Python How to use a global variable in a function
By:Roy.LiuLast updated:2019-08-11
In Python, we can use a global keyword to referring a global variable.
1. Review the below example :
a = 10 def updateGlobal(): a = 5 updateGlobal() print(a) # 10
Output – It will return a 10, instead of 5.
10
2. To modify the global variable “a”, add a global keyword like this :
a = 10 def updateGlobal(): global a a = 5 updateGlobal() print(a) # 5
Output
References
From:一号门
COMMENTS