Python Tutorial – How to Use Global Variables in a Function | Python Programs Examples

In this post we are going to use learn about how to use global variables in a function, its usage and basic examples of python programs.

Variables in Programming Language

Variables are defined as a place where the data is put to store in memory just take an example of your belonging before keeping them in the house we put them in a cupboard or a box and in this scenario that cupboard or box is variable those belongings are the data and house is the memory where we have cupboards or box kept.

Variable
data in variable

By looking at the given image we can conclude that our data is stored in variable and those variables could be used any number of times once our work is done with the data we do not need to keep stored data in a variable we can remove the data from variable and reuse the data again with certain conditions.

Variables always have a unique name in their range

  Types of Variables

There are two types of variables in terms of their range of access.

Types of variables

 Local Variable

They are the variables that have a limited range of access means if they are declared in a function that means they can be used only in that function such variables may have the same name as the name of the variable in some other function provided their range of access should meet at any point of time.

def myfunc():
  x = "fantastic"
  print("Python is " + x)

def myfunc1():

print("Python is " + x)

in the given example, we have x as a local variable in a function myfunc() function and it is being used also in the same range that is inside the function then it will run but in the case of myfunc1() we cannot use it as variable as it does not have the range so it will give error as x is not defined.

Moreover, if we want to use x then we have to define the variable again and assign the value again. And here the value of x in both the functions will be independent of each other and they would not be affected, and in both functions, the value of x could be different . provided the variables are declared inside the function.

   Global Variable

They are the variables whose value remains constant thought out the program. And the most important thing in a global variable is they are declared outside of any function so all the functions can use them as their own variable. However, the value of the global variable remains the same for all the functions until we manually change it and after changing in one function will be charged for another function Too for the next time.

  x = "value"
def myfunc():

  print("variable in function1 " + x)

myfunc():

print("variable in function2 " + x)

Now it will run for both the functions and print the output as mentioned as now x is a global variable.
Generally, we use the global variables we need to keep the value of certain variables constant though out the program, For example, the value of Pie which remains constant always so we can declare pie as global and the value could be used by all the functions present in class.
We could also have a global variable outside the class to use the same value of that variable in other classes too.

In the case of inheritance, we use the Global variable outside all the classes so that we can use the single variable in all the given classes to reduce memory as it required only one memory location if we use it, Whereas we need to store variables for all the functions

However, there are certain rules for naming the variable we need to keep in mind read all the rules of naming variables and learn more from  about Variables

https://betapython.com/how-do-i-create-variable-variables

to learn more:

https://docs.revenera.com/installshield20helplib/Subsystems/installshield20langref/Content/helplibrary/LangrefGlobal_vs_local_variables.htm#:~:text=Global%20variables%20are%20visible%20and,script%20that%20follow%20its%20declaration.&text=A%20variable%20is%20local%20if,function%20where%20they%20are%20declared.

Leave a Comment

%d bloggers like this: