+ 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

12th Nov 2020, 11:34 AM
King Samser
King Samser - avatar
12 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.
12th Nov 2020, 11:50 AM
Seb TheS
Seb TheS - avatar
+ 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.
14th Nov 2020, 11:14 AM
Chris Ng
Chris Ng - avatar
+ 4
Thank you very much
12th Nov 2020, 11:54 AM
King Samser
King Samser - avatar
+ 2
Help me please
12th Nov 2020, 11:34 AM
King Samser
King Samser - avatar
+ 2
This your questions ----
12th Nov 2020, 11:47 AM
samsul
samsul - avatar
+ 2
But can your explain more
12th Nov 2020, 11:51 AM
King Samser
King Samser - avatar
+ 2
Your welcome 😉
12th Nov 2020, 12:00 PM
Olivia
Olivia - avatar
+ 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
12th Nov 2020, 8:34 PM
Olorunsogo Abiodun Isaac
Olorunsogo Abiodun Isaac - avatar
+ 2
Olorunsogo Abiodun because x is defined in the global scope,it is a global variable and so can be used anywhere,including functions
13th Nov 2020, 2:49 AM
Boay.JS
Boay.JS - avatar
+ 2
Ok thank you for you information
13th Nov 2020, 3:02 AM
King Samser
King Samser - avatar
+ 2
Okey
14th Nov 2020, 11:14 AM
King Samser
King Samser - avatar