+ 5
What are local variables and global variables in python ??
I don't understand what is the difference between the code,and what is global variables.If local variables is a local code such as int... .what is global variables.If global variables is one global thing. Can your guys help me.I don't understand this thing
14 Answers
+ 8
Local variable is a variable, which can only be used inside the namespace it was defined.
Global variable is a variable, which can be used anywhere in the program.
+ 8
We strongly encourage using local variables, to improve readability and maintainability.
Since local variables are almost immediately used after they are declared, we know with certainty that we will never have to makes references to that value in later sections of your code.
They also naturally improve memory allocation, since they are quickly eligible for garbage-collection.
Global variables are almost never a good idea because it violates encapsulation! Which as others pointed out is accesible everywhere.
+ 4
Thank you very much
+ 2
Help me please
+ 2
This your questions ----
+ 2
But can your explain more
+ 2
Your welcome đ
+ 2
x=54
def multiply():
return x*2
multiply ()
print (x)
The X before the function is global while the X with the function is local.
Hope that helps
+ 2
Olorunsogo Abiodun because x is defined in the global scope,it is a global variable and so can be used anywhere,including functions
+ 2
Ok thank you for you information
+ 2
Okey