Python Variable Length Arguments Functions

📔 : Python 🔗

पिछले chapter में आपने Parameterized Functions के बारे में पढ़ा , इन functions को जब हम define करते थे तो हमें need के according parameter भी define करते थे , और उस function को Call करते समय हमें उसी के accordingly values भी pass करनी होती थी।


मतलब हमें ये पता होता था कि कितने arguments pass किये जायेंगे उसके according हम function parameters define करते थे और उसी के according function के अंदर logic लिखते थे। अब अगर हमें ये नहीं मालूम हो कि function call करते समय कितने arguments pass हो रहे हैं तो ?


इस तरह के functions को handle करने के लिए हमें Python ने facility provide की है Variable Length Argument Functions . जिसकी help से हम pass किये गए सभी arguments / values को easily handle कर सकते हैं। इसके लिए हमें function define करते समय parameter से पहले सिर्फ asterisk ( * ) prepend करना होता है।

Python Variable Length Arguments Function Syntax
def function_name(*args) :
  #do whatever you want.
  return something. #optional

इस एक single variable में function call किये गए time पर pass की गयी सभी values का tuple मिलता है , जिसे पर for loop या while loop की help से easily handle कर सकते हैं।

Python Variable Length Arguments Function Syntax

Copy Fullscreen Close Fullscreen Run
def do_sum(*args) :
    #check type.
    print('Type is :', type(args))
	
    #total number of passed arguments.
    print('Total no. of passed arguments is :', len(args))
	
    #now use for loop to sum the enetered values.
    sum = 0
    for num in args :
        sum += num
    print('Sum of passed values is :', sum)

#call the function.
do_sum(2,4,6,8,10,12)

print('---------------------------------')

#try to pass different values so that we can see difference.
do_sum(10, 20, 30, 40, 50, 60, 70, 80, 90, 100)
Output
C:\Users\Rahulkumar\Desktop\python>python function_args.py
Type is : <class 'tuple'>
Total no. of passed arguments is : 6
Sum of passed values is : 42
---------------------------------
Type is : <class 'tuple'>
Total no. of passed arguments is : 10
Sum of passed values is : 550

Example देखकर आप समझ सकते हैं कि कितनी ही values pass करने पर हम easily उन values को easily handle कर सकते हैं।

Python Variable Length Arguments Function With Named Arguments

ऊपर जो आपने example देखा वो named arguments type की value पर work नहीं करेगा , because जब हम argument को name के साथ pass करते हैं तो name के साथ value भी associate होती है।


Named argument को handle करने के लिए double asterisk (**) use करना पड़ता है , जो pass Named arguments की dictionary होती है। dictionary में key argument का name होता है और value उस variable को assign की गयी value .
See Example -

Copy Fullscreen Close Fullscreen Run
def do_sum(**args) :
    #check type.
    print('Type is :', type(args))	
    #see how passed data is arganised.
    print(args)
	
    #now use for loop to sum the enetered values.
    sum = 0
    for var in args :
        sum += args[var]
    print('Sum of passed values is :', sum)

#call the function.
do_sum(num1=10, num2=20, num3=30, num4=40)

print('-----------------------------------------------')

#change values and name.
do_sum(one=1, two=2, three=3, four=4, fifty=50, sixty=60)
Output
C:\Users\Rahulkumar\Desktop\python>python function_args.py
Type is : <class 'dict'>
{'num1': 10, 'num2': 20, 'num3': 30, 'num4': 40}
Sum of passed values is : 100
-----------------------------------------------
Type is : <class 'dict'>
{'one': 1, 'two': 2, 'three': 3, 'four': 4, 'fifty': 50, 'sixty': 60}
Sum of passed values is : 120

I Hope, कि अब आप समझ गए होंगे कि Python में parametrized functions को कैसे कैसे handle कर सकते हैं।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers