Default Arguments in Python

Default Arguments in Python

Python has a different way of representing syntax and default values for function arguments. Default values indicate that the function argument will take that value if no argument value is passed during function call. The default value is assigned by using assignment (=) operator.

पायथन में फंक्शन तर्कों के लिए सिंटैक्स और डिफॉल्ट मानों का प्रतिनिधित्व करने का एक अलग तरीका है। डिफॉल्ट मान इंगित करते हैं कि फंक्शन तर्क उस मान को ले जाएगा यदि फंक्शन कॉल के दौरान कोई तर्क मान पारित नहीं हुआ है। डिफॉल्ट मान असाइनमेंट (=) ऑपरेटर का उपयोग करके असाइन किया गया है।
Example:

def myFun (name="Gyancs School") :
    print ("hello", name, " Thank you")
myFun() 
myFun ("Pawan School")

Output:
hello Gyancs School  Thank you
hello Pawan School  Thank you

If we are not passing any name then only default value will be considered.
यदि हम कोई नाम नहीं दे रहे हैं तो केवल डिफॉल्ट वैल्यू पर विचार किया जाएगा।

Note: After default arguments we should not take non default arguments. Otherwise we will get an error message.
डिफॉल्ट तर्कों के बाद हमें गैर डिफॉल्ट तर्क नहीं लेना चाहिए। अन्यथा हमें एक त्रुटि संदेश मिलेगा।
Example:

def wish (name="guest", msg="good morning"): #Valid
    def wish (name, msg="good morning"): #Valid
        def wish (name="guest", msg) :   #InValid

Keyword Arguments – कीवर्ड तर्क

We can pass argument values by keywords i.e., by parameter names. Here the order of arguments is not important but number of arguments are must be matched.

हम कीवर्ड द्वारा तर्क मान पारित कर सकते हैं अर्थात् पैरामीटर नामों से। यहाँ तर्कों का क्रम महत्वपूर्ण नहीं है लेकिन तर्को की संख्या का मिलान किया जाना चाहिए।
Example 1:

def reqArgFun (name, msg) :
    print (name, msg)
reqArgFun (name="Gyancs", msg="Computer Engg Teachear")
reqArgFun (msg="Computer Engg Teachear", name="Gyancs")

Output:
Gyancs Computer Engg Teachear
Gyancs Computer Engg Teachear

Note: We can use both positional and keyword arguments simultaneously. But first we have to take positional arguments and then keyword arguments; otherwise we will get an error message.

हम एक साथ स्थिति और कीवर्ड दोनों तर्क का उपयोग कर सकते हैं। लेकिन पहले हमें स्थितिगत तर्क और फिर कीवर्ड तर्क लेना होगा, अन्यथा हमें एक एरर मैसेज मिलेगा।
Example 2:

def myFun (name, msg):
    print ("hel10", name, msg) 
myFun ("banana", "indian fruit")#valid
myFun ("mango", msg='yummy') #valid
    myFun (name="apple", "awesome") #Not valid

Output:
hello banana indian fruit
hello mango yummy
SyntaxError: positional argument follows keyword argument

Variable Length Arguments
वैरियेबल लेंथ तर्क

This is very useful when we do not know the exact number of arguments that will be passed to a function or we can have a design where any number of arguments can be passed based on the requirement such type of arguments are called variable length arguments.

यह बहुत उपयोगी है जब हमें पता नहीं चलता कि तर्कों की सही संख्या किसी फंक्शन को दी जाएगी या हमारे पास एक डिजाइन हो सकता है जहां आवश्यकता के आधार पर किसी भी प्रकार के तर्कों को पारित किया जा सकता है इस तरह के तर्कों को वैरियेबल लंबाई तर्क कहा जाता है।

We can declare a variable length argument with “*’ symbol as follows. हम ‘*’ सिम्बल के साथ एक वैरियेबल लंबाई तर्क की घोषणा कर सकते हैं।

Syntax: def myFun(*n)

We can call this function by passing any number of arguments including zero number. Internally all these values are represented in the form of tuple.

हम जीरो नम्बर सहित किसी भी तर्क को पारित करके इस फंक्शन को कॉल कर सकते हैं। आंतरिक रूप से इन सभी मूल्यों का प्रतिनिधित्व ट्यूपल के रूप में किया जाता है।
Example 1:

def sum (*n) :
    total = 0 
    for a in n:
        total = total + a 
    print (" The sum = ", total)
sum ()
sum (10) 
sum (1,2,3) 
sum (1.2, 3.4, 55, 6)

Output:
The sum =  0
 The sum =  10
 The sum =  6
 The sum =  65.6

Note: We can also mix variable length arguments with positional arguments.

नोट: हम पोजीशनल तर्क के साथ वैरियेबल लंबाई तर्क भी मिश्रण कर सकते हैं।
Example 2:  

def myFun (n, *s) :
    print (n)
    for a in s:
        print (a) 
print ("myFun (10) output : ") 
myFun (10)
print ("myFun (10, 20, 30, 40) output : ") 
myFun (10,20,30,40) 
print ("myFun (10, 'a', 20, 'b') : ")
myFun (10, "a", 20, "b") 

Output:
myFun (10) 
Output:
10
myFun (10, 20, 30, 40)
Output :
10
20
30
40
myFun (10, ‘a’, 20, ‘b’) :
10
a
20
B

Note: After variable length argument, if we are taking any other arguments then we should provide values as keyword arguments.

वैरियेबल लंबाई तर्क के बाद, अगर हम कोई अन्य तर्क ले रहे हैं तो हमें कीवर्ड तर्क के रूप में वैल्यू प्रदान करना चाहिए।
Example 3:

def myFun (*n, s) :
    for a in n:
        print (a) 
    print(s)
myFun ( 'A', 'B', s=100)

Output:
A
B
100

If we use like below statement, we will get an error message.

यदि हम नीचे दिए गए विवरण की तरह उपयोग करते हैं, तो हमें एक एरर मैसेज मिलेगा।

myFun ( ‘A’, ‘B’, 100)

TypeError: myFun () missing 1 required keyword-only argument: ‘s’

Note: we can declare keyword variable length arguments also. For this we have to use **. Like below statement.

हम कीवर्ड वैरियेबल लंबाई तर्क भी घोषित कर सकते हैं। इसके लिए हमें ** का उपयोग करना होगा। नीचे दिए गए स्टेटमेंट की तरह

def myFun (**n)

We can call this function by passing any number of keyword arguments. Internally these keyword arguments will be stored inside a dictionary type.

हम इस फंक्शन को किसी भी संख्या में कीवर्ड तर्क पास करके कॉल कर सकते हैं। आंतरिक रूप से ये कीवर्ड तर्क एक डिक्शनरी टाईप के अंदर संग्रहीत किए जाएंगे।
Example 4:

def myFun (**n):
    for a, b in n.items () :
        print (a, "=", b)
print('calling myFun (n1=100, n2=200, n3=300, n4=400)')
myFun (nl=100, n2=200, n3=300, n4=400) 
print('calling myFun (rno=1, name="subhash", marks=89, subject="python")') 
myFun (rno=1, name="subhash", marks=89, subject="python")

Output:
calling myFun (n1=100, n2=200, n3=300, n4=400)
nl = 100
n2 = 200
n3 = 300
n4 = 400
calling myFun (rno=1, name=”subhash”, marks=89, subject=”python”)
rno = 1
name = subhash
marks = 89
subject = python
Example 5:

def myFun (a, b, c=300, d=400):
    print (a, b, c, d)
myFun (3,2) 		# 3 2 300 400
myFun (1,2,3,4) 	# 1 2 3 4 3 4
myFun (3,4, d=100) # 300 100
myFun (d=2, a=3, b=6)  #33 6 300 2
myFun()			# Invalid

Output:
3 2 300 400
1 2 3 4
3 4 300 100
3 6 300 2
Traceback (most recent call last):
  File “C:\Users\HP\Desktop\1.py”, line 7, in <module>
    myFun()                # Invalid
TypeError: myFun() missing 2 required positional arguments: ‘a’ and ‘b’

Chapter wise Model Paper Link

About Me