Important Notice:

*args

*args

22 views 1 min read

*args :-

Python में *args का उपयोग किसी function में किसी भी संख्या में Positional Arguments प्राप्त करने के लिए किया जाता है। Function के अंदर ये सभी arguments एक Tuple के रूप में उपलब्ध होते हैं।

English Definition: *args allows a function to accept any number of positional arguments. These arguments are collected into a tuple.

Syntax:

def function_name(*args):
    # statements
    pass

Example 1: Display Multiple Arguments

def display(*args):
    print(args)

display(10, 20, 30, 40)

Output:

(10, 20, 30, 40)

Explanation:

  • *args एक tuple बनाता है।

  • args में सभी positional arguments store होते हैं।

  • args नाम अनिवार्य नहीं है; *numbers या कोई अन्य valid name भी इस्तेमाल कर सकते हैं।

Example 2: Find the Sum of Multiple Numbers

def calculate_sum(*args):
    total = sum(args)
    return total

print(calculate_sum(10, 20))
print(calculate_sum(10, 20, 30))
print(calculate_sum(1, 2, 3, 4, 5))

Output:

30
60
15

Example 3: Access Arguments Using a Loop

def show_numbers(*args):
    for number in args:
        print(number)

show_numbers(5, 10, 15)

Output:

5
10
15

Related Notes