Javaexercise.com

What are the scopes of Variable in Python?

Python is a high-level, interpreted programming language and is one of the most popular programming languages present in the software industry.

One of the most important concepts in python is that of scopes of variables generally with respect to functions. Let us have a look at it in detail.

What are variables in Python?

In python, variables are the identifiers that programmers use to store data of different types. They basically act as pointers or references to the memory area where the data is stored.

One difference here is that we do not assign a static data type to a variable, unlike other common programming languages.

Thus, python has dynamically typed variables. Also unlike other programming languages, we don’t need to explicitly have a statement to create a variable to mention its data type.

In python, whenever we assign a value to a variable for the first time, that acts as the initialization for it. 

What are variable scopes in Python?

Variables in python can only be accessed within the blocks that they were initialized.

These blocks or regions are also termed as the scope of the variable. 

In python, variable scopes with respect to functions are of two different types corresponding to local and global variables respectively.

Local variables are the ones initialized inside the function whereas global variables are the ones initialized outside the function.

Let us have a look at each of them in a bit more detail:

Local variables in Python

In this example, we look at the usage of the concept of local variables with respect to a function definition and call.

The function named func defined and called here is a simple function that initializes a variable with a value of 1 and prints this value as well.

No parameters are passed to this function.

# Defining the function
def func():

  # Initializing variable a with value 1
  a = 1

  # Printing the value of a
  print(a)

func() # calling the function

Output

1

Global variables in Python

In this example, we look at the usage of the concept of global variables with respect to a function definition and call.

The function named func defined and called here is a simple function that prints the value of a variable a.

An important difference from the previous example here is that the variable a has been initialized outside the definition of the function func, specifically before the function call happens.

Let us take a look at the python code and corresponding output for this method:

# Defining the function
def func():

  # Printing the value of a
  print(a)

# Initializing the variable a with value 1 outside the function
a = 1

func() # Calling the function

Output

1

The difference between local and global variables might not be apparent here but one important advantage of global variables is that they need to be initialized only once and can be used multiple times in function calls. 

Let us take a look at the python code and corresponding output for this method:

# Defining the function
def func():

  # Printing the value of a
  print(a)

# Initializing the variable a with value 1
a = 1

func()  # Calling the function
func()  # Calling the function twice

Output

1

Global variables vs local variables in Python

In this example, we look at the usage of the concept of global variables versus local variables with respect to a function definition and call which is similar to the example used earlier.

The function named func defined and called here is a simple function that initializes a variable a to have a value of 2 and prints its value.

An important difference from the previous examples here is that the variable a has been initialized outside the definition of the function func as well with a value of 1, specifically before the function call happens. 

What happens, in this case, is that the value of the innermost initialization is taken into consideration.

The initialization inside the function definition is an inner one when compared to the initialization outside the function definition. Thus, the value of 2 is considered and printed instead of the value of 1.

# Defining the function
def func():

  # Initializing variable a with value 2
  a = 2  # local variable

  # Printing the value of a
  print(a)

# Initializing the variable a with value 1 outside the function
a = 1   # global variable

func()

Output

1

Conclusion

In this topic, we have learned the use and advantages of variable scopes in a Python program, following some simple running examples of programs, thus giving us an intuition of how this concept could be applied in real-world situations. Feel free to reach out to [email protected] in case of any suggestions.