Python Function Arguments

Introduction

Function arguments are the values passed to a function when calling it. Python supports various types of arguments that give you flexibility in writing functions.

Function arguments वे मान होते हैं जो function कॉल करते समय पास किए जाते हैं। Python में विभिन्न प्रकार के arguments होते हैं जो functions लिखने में लचीलापन देते हैं।

Example 1: Positional Arguments
def sum(name, age):
    print(f"Hello {name}, you are {age} years old.")

sum('Rohit', 25)

Arguments passed are assigned based on their position.

Arguments उनकी स्थिति के अनुसार parameters को असाइन होते हैं।

Output:

Hello Rohit, you are 25 years old.
Example 2: Keyword Arguments
def sum(name, age):
    print(f"Hello {name}, you are {age} years old.")

sum(age=30, name='Sonia')

Arguments are passed using parameter names; order does not matter.

Arguments parameter नामों के साथ पास किए जाते हैं; क्रम महत्वपूर्ण नहीं होता।

Output:

Hello Sonia, you are 30 years old.
Example 3: Default Arguments
def sum(name, age=20):
    print(f"Hello {name}, you are {age} years old.")

sum('Amit')
sum('Nisha', 28)

Parameters can have default values used if no argument is passed.

Parameters को default मान दिया जा सकता है जो argument न देने पर इस्तेमाल होता है।

Output:

Hello Amit, you are 20 years old.
Hello Nisha, you are 28 years old.
Example 4: Variable-length Positional Arguments (*args)
def sum_all(*args):
    total = 0
    for num in args:
        total += num
    print(f"Sum is {total}")

sum_all(1, 2, 3, 4)

<code>*args</code> collects all positional arguments as a tuple.

<code>*args</code> सभी positional arguments को tuple में संग्रहित करता है।

Output:

Sum is 10
Example 5: Variable-length Keyword Arguments (**kwargs)
def print_info(**kwargs):
    for key, value in kwargs.items():
        print(f"{key}: {value}")

print_info(name='Raj', age=24, city='Delhi')

<code>**kwargs</code> collects keyword arguments as a dictionary.

<code>**kwargs</code> सभी keyword arguments को dictionary में संग्रहित करता है।

Output:

name: Raj
age: 24
city: Delhi