Home » Python » Types of variables in Python

Types of variables in Python: Global and local variables

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.

Syntax of a function in Python

def function_name():
code block inside function

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.


Python Local Variables

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:

Example: Python Local Variables
#This function prints value of a global variable
def display_string(): #start of function scope
print(s) #end of function scope #Global scope
s = "Welcome to CodeRepublics" display_string() #calling the function

Output

Welcome to CodeRepublics


Python Global Variables

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.

Example: Create a Global Variable
x = "global variable" def func_display(): print("x inside:", x) func_display print("x outside:", x)

Output

x inside: global variable
x outside: global variable

In the above code, a global variable is created and used by multiple functions.

Using the same name for Local and Global variable

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:

Example: Using the same name for local and global variable
a = 50 def demo(): a = 100 print("Local a:", a) demo() print("Global a:", a)

Output

Local a: 100
Global a: 50

In the example, inside the function, the local variable's value gets printed rather than the global variable.


Create a global variable inside a function

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.

Example: Create Global variable inside a function
#create a function:
def myfunction(): global x x = "hello" #Call the function:
myfunction() #x is a global variable, and accessible in the global scope.
print(x) #Printing x in global scope

Output

hello

Note: To create a global variable inside a function, it must be declared first and then gets assigned a value.

Example of changing global variable's value inside a function

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.

Example: Changing Global Variable's value inside function
# Python program to modify a global
# value inside a function
a = 95 def change(): # using global keyword
global a # increment value of a by 5
a = a + 5 print("Value of a inside function :", a) change() print("Value of a outside a function :", a)

Output

Local a: 100
Global a: 100