#!/usr/bin/python# Filename:func_global.pydef func (): Global x print ' x is ', x x = 2 print ' Changed loca L x to ', XX = 50func () print ' Value of x is ', x>>>>x are 50Changed Global X to 2Value of X is 2
How it works
The global statement is used to declare that X is global-so when we assign a value to x within a function, the change is reflected when we use the value of x in the main block.
You can use the same global statement to specify multiple global variables. For example, Global x, Y, Z.
#!/usr/bin/python# Filename:func_local.pydef func (x): print ' x is ', x x = 2 " print ' Changed local x to ', XX = 50func (x) print ' x is still ', x>>>>>x are 50Changed local x to 2x are still 50
In the function, the first time we use the value of X, Python uses the value of the formal parameter declared by the function.
Next, we assign the value 2 to X. X is the local variable of the function. So, when we change the value of x within a function, the x defined in the main block is unaffected.
"Python" Global