Recursion function in Python

In this tutorial, you will learn about recursion function in python with example. The term recursion involves process of calling itself in definition. The physical word would be to place two parallel mirrors facing each other. If any object is present in between the mirrors then it is reflected recursively. Recursion also work like the loop. In recursion function term the function will call continue to call itself and repeat its behavior until condition met to return the result. The recursive function is defined in term of itself via self referential expressions. If we want to repeat the task, we usually think about for and while loop. In some cases the function makes more sense to use recursive loop and convert any loop to recursion. The condition which is repeatedly followed and not stopped is called as base condition. Base condition plays an important role in every recursive program otherwise it will continue to execute like infinite loop.

Example:-
     We have function xyz() in body of xyz() there is call to xyz().

Create recursive function:-

For this we will take python recursive example to calculate no of pythons recursion factorial.
The no n is n*(n-1)*(n-2).
The recursive function is called to itself. Following is explained as
Factorial of (4)

  1. 4*3
  2. 3*2
  3. 2*1

The output is 24.

The example is with recursive function.
Example:-
def factorial(n):
f=1
while n>0:
f*=n
n=1
print(f)
factorial(4)
O/p:-
24

Scope of variables within functions:-

Variables in python are reserved memory location to store value.
Every value in python has the data type.
Variable in python program gives data to computer for process.
Variables can be declared by any name aa, bb, etc.
To understand the scope of variables we need to understand the meaning of variables.
There are the references pointers to access the memory.
The scope of variable refers to the place that you see or access the variable.
The scope of variable is the part of code visible also u can assign the variables by “=” sign.
You don’t need to use the prefixes for them.

Global variable are the one that defined and declared outside a function need to use them inside a function.
Example:-
def f():
print s
s=”I love python”


f()

O/p:-
I love python
Variable declaration example in python:-
A=20
Print a
The redeclaration of the variable is also possible after declaration.
There are two types of variables in python:-

  1. Local variables.
  2. Global variables.

Local variables:-

If we want to use in specific function or method we use local variables.
Example:-
def foo():
y = "local variable"
print(y)
foo()

O/p:-
Local variable

Global variables


In python the variables declared outside the function or in the global scope are known as global variable.
 Global variables can be accessed inside or outside function.
Example:-

x = "Python"

def foo():
    print("x inside :", x)

foo()
print("x outside:", x)

O/p:-
x inside : Python
x inside :Python

In above example we have x as global variable and define foo() to print the global
Variable x.
Then call foo() which will print value of x.

Now the both variables are declared in same code.
Example:-

    x = "hello"
    def foo():
    global x
    y = "python"
    x = x * 2
    print(x)
    print(y)
    foo()

O/p:-

hello hello
python

Additional Services : Refurbished Laptops Sales, Python Classes, Share Market Classes And SEO Freelancer in Pune, India