GLOBAL & LOCAL VARIABLE - PYTHON PROGRAMMING

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello friends welcome back to our Channel so in the previous session we have started the functions concept in Python programming in today's session let us have a look on a small concept that is the difference between the local and global variables see the main difference between the local and the global variables is scope scope of local variables will be fully beat in the function where the variables have been declared so here we can call it as a little dot initialized right next global scope will be and right so how this variable can be treated as a global variable how these variables can be treated as a local variables see so in C language we have seen that whatever the variables we are declaring upon the main function will be considered as a global variables so here also the compost variables that is above the user defined functions are take it as a global variables see example let us take an example J is equal to some 20 right next so we have seen in the previous session every other different functions should start with da f keyboard DF are open function they write display so here there are room curly braces so we have to represent a block of statements by using indentation so that can be done by placing the corn after the header now let us write the 3 e so this is this nster you have a different function so here I am writing the mouth of function that is display so let us write here inside user defined function display just this is a function call next I am writing the print I am printing the outside the user-defined so this is the small program which use an example for global variables see here always a function that is a user-defined function should be written and the topmost right so after the user-defined function only we have to write the function call so here the variables which are initialized above the user-defined function are treated as a global variable so this one will be treated as a global variables and after the user-defined function the function call should be written right so this is the function 1 so this is the inside the user-defined function now a is written just a other the other defined function so a is a global variable so in Evin you can be used in the entire program that means it can be used in both in user-defined function and the main function right so for easy purpose just we call this program as a main function right this is a main function and this is the user Department right so here is output first it will displace inside the user-defined function 20 and next if you think outside the user-defined function 20 so here in the user-defined function we have not declared more initializing any available even in the main function itself we are not declaring any variable we are declaring and initializing a variable just able to use a different function which will because variable and that value can be used in both use a different function and the green function right so who doesn't this global variable so what are the variables which are declared in within the function either it may be user defined function or it's a main function so that scope will be limited to that function only so in which function we have declared a variable that will be only used in that particular function so for example here let us take some B is equal to 20 display B is equal to 20 inside the user-defined function let it be right here right one more thin function print local variable B right so what is the output here first display function will be called the function call will be executed so then so b is equal to 20 print inside user-defined function a is equal to 20 will be printed right let us take B is equal to 30 next to print local variable so variable will be printed as 30 and then I'll say they were the different function J will be printed a story if if we write the same staple here local variable B so it will result and because here the B is not initialized in the main function and businessman is only in the user-defined function so it is a local variable so this one is a local variable where this code can be used only within the user-defined function so it cannot be used in main function or if a variable is declared in main function then it bc is equal to 40 and if you try to print the C in a user-defined function here also it is 11 because he has seen local variable so it will be used only within the main function so that is the main difference between the local variable get it globally right see let us see one more example is equal to tell diff display print global it next B is equal to 20 print local right the main function display next C is equal to 40 print local see print global E right so this is a small example so just we have seen this one so here B is a local variable so here it will be printed as 10 because he is a global variable local variable you to spin 20 here it will be printed 40 it will be printed and so here is a local variable I forget a local variable and B and C other local variables so bees are variable for the user-defined function and C is a local variable for main function no so what is the value to be printed if the local variable and the global variable are of same name right so if the variable is also having the same name a is equal to 40 here we are printing a print a right so if the local and a global variable share the same name the first preference will be to the local variables so here yeah is it 10 which is a global variable and here again we are Picard yay which is a local variable and we are trying to print a in the absence of this one a will be printed as 10 because it is a global variable so with the presence of a local variable the first preference the first the first preference is to local variable if local and global having same name if the local and global are having same things the first preference is to lower than you now here yeah is a local variable and as well as is a global variable now if you right click in this main function if you print only the local variable output that is here and here if you print a is equal to 20 is equal to 20 here the egg will be printed has only 20 write C C is equal to 40 D is equal to a plus C D C here C is equal to 20 both of our variables a and B a is equal to 20 C is equal to 20 both other global variables right so coming to the main function it will display a will be displayed so 20 will be displayed and after that here a is equal to 40 so it will be 40 now and then this is equal to a plus C D is equal to a a value is 40 because the first preference is to local variable so 40 plus C C is not initialized so see a proper variable we have to consider the global value that is 20 so the output will be 60 so here 20 will be printed here again 40 will be printed also here 40 will be printed and that is a 60 will be printed hope you understood the difference between the local and global variables so the main difference between these two is scope where we have to use these variables right have you understood this concept - how clear so if you really understood my sessions Mike my sessions and if you are having any doubts regarding this Python programming feel free to post your doubts in the comment section so the definitely I will try to care for our your thumbs right so share my sessions with your friends and don't forget to subscribe to our Channel thanks for listening thank you very much
Info
Channel: Sundeep Saradhi Kanthety
Views: 45,377
Rating: 4.9414506 out of 5
Keywords: SUNDEEP, SARADHI, KANTHETY, PYTHON, PROGRAMMING, HIGH-LEVEL, LANGUAGE, PORTABLE, FEATURES, APPLICATIONS, INSTALLATION, INTERPRETER, PYTHON SHELL, PYTHON SCRIPT, INTRODUCTION, BEGINNERS, GUI, GAMING, WEB DESIGNING, PYTHON 3.7.0, PLATFORM INDEPENDENT, CASE SENSITIVE, SIMPLE, LOCAL, GLOBAL, SCOPE, WITHIN FUNCTION, OUTSIDE FUNCTION, FUNCTIONS, local variable, local variable vs global variable
Id: gRTVLZuidG0
Channel Id: undefined
Length: 13min 18sec (798 seconds)
Published: Sat Oct 06 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.