Python Scopes

Python Scopes

In Python Programming language variables that can only reach the area in which they all are defined are known by the term of scope.

As you can easily understand from the word scope, that it means range or extent. So in Python Programming language, we use the scope to access and call variables for a selected region or for a selected range of code. The scope concept in python is generally presented using a rule which is known as the LEGB rule which stands for Local, Enclosing, Global, and Built-in scopes.

Types of scopes in Python

There are two types of scope in the Python Programming language. They are stated below:-

  1. Local Scope
  2. Global Scope

Both these scopes have different roles and are used at different times according to the needs of the program. Now we will understand when and how to use both the scopes i.e. Local Scope and Global Scope.

Local Scope

A variable in python which is created inside a function always belongs to the local scope of that function. And the function inside the scope can be used only inside the function. Let's take an example of Local scope:-

So the value of x is local for func1 and it can be easily used inside a function so we get the output as 10 which is the value of x because x is a local variable and can be accessed locally but only in the same function.

Global Variable

Global Scope in Python contains a Global Variable which is created in the main body of the python code. Global variables are available from within any scope, global and local. You can call a global function when you want to implement it and use it as per your program need. Let's understand better about Global scope using an example:-

So as you can see that we got the following output and here x acts as a global variable and it has executed in the function and even after the function and this is the function of global scope that you can call the global variable throughout the program instead of calling inside the function.

Recursion

Recursion in python is a process of repeating items again and again in a self-similar way. Basically, Recursion allows you to call a particular function inside the same function, and thus the function is known by the name of Recursive Function. In recursion, the function can call itself directly or indirectly.

Advantages of Recursion

  1. A complicated and big function can be split into small sub-problems with the help of recursion.
  2. Recursive functions make the code simple and effective.

Disadvantages of Recursion

  1. The functions which are created for recursion are proven challenging to debug.
  2. Recursive functions take a lot of memory and time for calling them.

The basic examples of Recursion are the Fibonacci series and the factorial of a number. Let's take a look at the example of Recursion:-

Fibonacci series through Recursion

So now we will take the example of Fibonacci series:-

As can see that we had obtained the Fibonacci series.

Factorial of a number using Recursion

Now we will find the factorial of a number in python with the help of recursion:-

So we had obtained factorial of a number.

Now we had got a brief knowledge about python scopes and recursion functions.

If you need more help about scope in python then refer to this video -

For recursion refer to this video -

NOTE:- The amazing artwork used in this article is created by - Karol Cichoń.

Thanks for Reading. Hope you liked it. Many more to come in near future.