Blog

Higher order functions in python in hindi - Bharosewale

Posted By Naveen Singh on 26 May 2023

python-program
higher-order-functions-in-python-in-hindi

 

Python में Higher order functions क्या होते हैं?( What are Higher order functions in python?)

 

Higher order function एक ऐसा function होता है जो argument के रूप में एक या एक से अधिक function लेता है या फिर result के रूप में function को return करता है। यह functional

(कार्यात्मक) programming की बहुत महत्वपूर्ण concept में से एक है। देखा जाए तो यहां पर बहुत सारे built-in higher order functions python में उपलब्ध हैं।

 

Python में map() function क्या होते है?( What are map() function in python?)

 

Map() function-:

 

Python में map() एक ऐसा function है जो iterable ( tuple,lists etc) के प्रत्येक item पर function लागू करने के बाद result देने के लिए एक iterator के रूप में कार्य करता है। इसका उपयोग तब

किया जाता है जब आप सभी चलने योग्य तत्वों के लिए single transformation function लागू करना चाहते हैं। iterable और function को python में map के argument के रूप में pass किया जाता

है।

Syntax of map() function -:

map(function,iterable)

 

Example-:

 

# Defining a function

def add(a):

   return a + 2

# Using the map function

x = map(add, (2,3,4,5,6))

print (x)

print(list(x))

 

Output:

<map object at 0x6fbfaba070>

[4, 5, 6, 7, 8]

 

Function: यह एक transformation function है जिसके through iterable के सभी items को pass किया जाता है।

Iterable: यह एक iterable (sequence, collection जैसे list या tuple ) है जिसे आप map करना चाहते हैं।


 

Python में filter() function क्या होते हैं?( What are filter function in python?)

 

Filter() function-:

 

filter() function दिए गए sequence को एक function की मदद से filter करता है जो sequence के प्रत्येक element को test करके पता करता है कि वो सही है की नही।

 

syntax:

filter(function, sequence)

function: function जो test करके पता करता है कि sequence का प्रत्येक element सही है या नहीं।

sequence: sequence जिसे filter करने की आवश्यकता होती है, यह किसी भी iterator के set, lists, tuples या container हो सकते हैं।

 

Example-:

# function that filters vowels

def my_fun(alphabet):

   letters = ['a', 'e', 'i', 'o', 'u']

   if (alphabet in letters):

      return True

   else:

      return False

# sequence

sequence = ['b','h','a','r','o','s','e','w','a','l','e']

# using filter function

filtered = filter(my_fun, sequence)

print('The filtered letters are:')

for x in filtered:

   print(x)

 

Output-:

The filter alphabets are:

a

o

e

a

e


 

Python में reduce() function क्या होते हैं?( What are reduce function in python?)

 

Reduce() function-:

 

Reduce() function का उपयोग हम इसके argument में दिए गए function को पास करे गए sequence में उल्लिखित सभी list elements पर लागू करने के लिए किया जाता है। यह function "

functools "module में defined किया गया है।

 

Example-:

 

import functools

# initializing list

my_list = [1,2,3,4,5]

# using reduce to compute sum of list

print("The sum of  list elements is : ", end="")

print(functools.reduce(lambda x,y: x+y, my_list))

 

Output-:

 

The sum of  list elements is : 15


 

Comments