A: First look at a small program
<strong><span style= "FONT-SIZE:18PX;" >k = 0;def Changek (): k = ten; Changek () Print k</span></strong>
define a variable K
Define a function Changex
Within Changex, assign 10 to K
Print X
Execution results are: 0
Note: K is a global variable, and another local variable is declared in Changex. This k non-K, after the execution of Changex, the function of the inside of the K disappears. Similar to Java.
Second: If a global variable is used inside a function
<strong><span style= "FONT-SIZE:18PX;" >k = 0;def Changek (): global k k = 10changek () print k</span></strong>
Three: summary:
1. Global---Define variables as globals. You can change the value of a variable within a function by defining it as a global variable.
2. A global statement can define multiple variables at the same time, such as Global X, Y, Z
Python Learning: Scopes