This article mainly introduces the function scope of Python variables. The example analyzes the definition and scope of variables in Python, which is an important basic skill in Python learning, for more information about the scope of the Python variable, see the example in this article. Share it with you for your reference. The details are as follows:
# Coding = UTF-8 # variable scope: global z # use the global variable z = 1 # assign a value to the global variable x = 99 # x when declaring the global variable def foo (y ): # y and z are assigned values in the function: Partial # Partial Region z = x + y # x is not assigned values, so it is the global return zdef bar (y ): global z = x + y return zprint foo (1) # result = 100 print z # result = 1 print bar (1) # result = 100 print z # result = 100
I hope this article will help you with Python programming.