IKH

Types of Functions

Recursive Functions

  • A function that calls itself is known as recursive function.
  • The main advantages of recursive functions are:
    • We can reduce length of the code and improves readability.
    • We can solve complex problems very easily.

Question

Write a python function to find factorial of given number with recursion.

Python
def factorial(n):
  if n==0:
    result=1
  else:
    result=n*factorial(n-1)
  return result
print("Factorial of 4 is :",factorial(4))
print("Factorial of 5 is :",factorial(5))

Output

PowerShell
Factorial of 4 is : 24 
Factorial of 5 is : 120

Anonymous Functions (Lambda Function)

  • Sometimes we can declare a function without any name, such type of nameless functions are called anonymous functions or lambda functions.
  • The main purpose of anonymous function is just for instant use (i.e for one time usage).

Example

Python
squr = lambda x: x*x
num=squr(2)
print(num)

Output

PowerShell
4

Remark

  • Lambda function internally returns expression value and we are not required to write return statement explicitly.
  • Sometimes we can pass function as argument to another function. In such cases lambda functions are best choice.
  • We can use lambda functions very commonly with filter(), map() and reduce() functions because these functions expect function as argument.

Normal Function

  • We can define by using def keyword.

Example

Python
df squr(num):
  return num*num
  
n = squr(4)
print(n)

Output

PowerShell
16

filter()

  • We can use filter() function to filter values from the given sequence based on some condition.

Question

Program to filter only even numbers from the list by using filter() function?

Python
def isEven(x):
  if x%2==0:
    return True
  else:
    return False
l=[0,5,10,15,20,25,30]
l1=list(filter(isEven,l))
print(l1)

Output

PowerShell
[0, 10, 20, 30]

2nd Method

Python
l=[0,5,10,15,20,25,30]
l1=list(filter(lambda x:x%2==0,l))
print(l1)

Output

PowerShell
[0, 10, 20, 30]

map()

  • For every element present in the given sequence, apply some functionality and generate new element with the required modification. For this requirement we should go for map() function.

Example

For every element present in the list perform double and generate new list of doubles.

Python
l=[1,2,3,4,5] 
def doubleIt(x): 
  return 2*x 
l1=list(map(doubleIt,l)) 
print(l1)

Output

PowerShell
[2, 4, 6, 8, 10]

2nd Method

Python
l=[1,2,3,4,5]
l1=list(map(lambda x:2*x,l))
print(l1)

Output

PowerShell
[2, 4, 6, 8, 10]

Example

To find square of the given number

Python
l=[1,2,3,4,5]
l1=list(map(lambda x:x*x,l))
print(l1)

Output

PowerShell
[1, 4, 9, 16, 25]
  • We can apply map() function on multiple lists also. But make sure all list should have same length.

Example

Add elements of two different list and create new list?

Python
l1=[1,2,3,4]
l2=[2,3,4,5]
l3=list(map(lambda x,y:x+y,l1,l2))
print(l3)

Output

PowerShell
[3, 5, 7, 9]

reduce()

  • reduce() function reduces sequence of elements into a single element by applying the specified function.
  • reduce() function present in functools module and hence we should write import statement.

Example

Python
from functools import *
l=[10,20,30,40,50]
result=reduce(lambda x,y:x+y,l)
print(result)

Output

PowerShell
150

Example

Python
from functools import * 
l=[10,20,30,40,50]
result=reduce(lambda x,y:x*y,l)
print(result)

Output

PowerShell
12000000

Example

Python
from functools import *
result=reduce(lambda x,y:x+y,range(1,101))
print(result)

Output

PowerShell
5050

Everything is an object

  • In python every thing is treated as object.
  • Even functions also internally treated as objects only.

Example

Python
def f1():
  print("Hello") 
print(f1) 
print(id(f1))

Output

PowerShell
<function f1 at 0x00419618>
4298264

Nested Functions

  • We can declare a function inside another function, such type of functions are called nested functions

Example

Python
def outer():
  print("outer function started")
  def inner():
    print("inner function execution")
  print("outer function calling inner function")
  inner()
outer()

Output

PowerShell
outer function started
outer function calling inner function
inner function execution
  • In the above example inner() function is local to outer() function and hence it is not possible to call directly from outside of outer() function.

Note

  • A function can return another function.

Example

Python
def outer(): 
  print("outer function started") 
  def inner(): 
    print("inner function execution")
  print("outer function returning inner function") 
  return inner 
f1=outer() 
f1() 
f1() 
f1()

Output

PowerShell
outer function started
outer function returning inner function
inner function execution
inner function execution
inner function execution

Question

What is the differences between the following lines?

  • f1 = outer
  • f1 = outer()

Answer

In the first case for the outer() function we are providing another name f1 (function aliasing). But in the second case we calling outer() function, which returns inner function.

Function Aliasing

For the existing function we can give another name, which is nothing but function aliasing.

Example

Python
def wish(name):
  print("Good Morning:",name)
 
greeting=wish
print(id(wish))
print(id(greeting))
greeting("ajay")

Output

PowerShell
140210281065472
140210281065472
Good Morning: ajay

Note

  • In the above example only one function is available but we can call that function by using either wish name or greeting name.
  • If we delete one name still we can access that function by using alias name.

Example

Python
def wish(name):
  print("Good Morning:",name)
greeting=wish
greeting('ajay')
wish('ajay')
del wish
greeting('amit')

Output

PowerShell
Good Morning: ajay
Good Morning: ajay
Good Morning: amit

Ungraded Questions

Get ready for an exhilarating evaluation of your understanding! Brace yourself as we dive into the upcoming assessment. Your active participation is key, so make sure to attend and demonstrate your knowledge. Let’s embark on this exciting learning journey together!


Name
Email
Phone

Report an error