Variables are of two types Global and local variables. The reason for this categorization of variables is their different levels of accessibility. The type of variable depends on whether we are able to access a variable on a certain block of code or not.
To understand the concept of global and local variables, you should be familiar with functions in a programming language. No! function here doesn't mean 'what a programming language can do.' Instead, it is a block of code in a programming language.
A block of code to perform a specified operation is known as function. For example, a function of addition can have a code block that specifies variables, values, logic and display output after addition.
The def
keyword defines a function. Now, you know what a function is, it will be helpful for you to understand global and local variables easily.
A function can have variables inside it called local variables. A local variable is the one that gets declared inside a function
. The important thing to notice is that, this local variable is unavailable for the code outside the function in which it is declared. Here, declaration means assigning a value to a variable for the first time.
In python, a function's scope/body is defined by the indentations. The scope of a function is its whole body, i.e., all the code written in a function within indentation is its scope. So, the local variables can only be used within a function's scope.
Let's look at an example of local variables in python:
Python Global variables are accessible all over a python script. In simple terms, if you declare a variable outside a function, then it is not limited within the scope of any function, as it is not declared inside a function. The advantage of the Global variable is that it can be accessed from inside or outside of a function.
Using global variable is helpful if there is a common value getting used multiple times in a python script. For example: Let's take a Global variable, name="Jack"
. This name, 'jack,' will be used by multiple functions for their own operation. So instead of creating several local variables with value 'Jack', functions can use a global variable. It saves memory space and time also.
Let's see an example of how a global variable is created in Python.
In the above code, a global variable is created and used by multiple functions.
You can create a local variable with same name as a global variable. It is helpful when you want a similar name for local variable but with different value.
In case of conflict of same variable names, function will give priority to the local variable's value. Lets look at an example:
In the example, inside the function, the local variable's value gets printed rather than the global variable.
Global variables in python can be created inside a function also. Remember that although it is declared inside a function, it doesn't mean that it can't be accessed outside that functio. Otherwise, it would be called a local variable. Just the place of declaration of the global variable is changed but not its properties. A global variable, regardless of its place of declaration, can be accessed globally within the script.
Inside a function, to declare a global Variable, global
keyword is used. For example: global x
.
To change a global variable's value inside a function, you need to use a global
keyword with the variable name. The syntax is similar to creating a global variable, but the difference is that now you will change the value rather than assigning the first value.
Follow Us:
code block inside function