Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python Functions
Next article icon

In Python, *args and **kwargs are used to allow functions to accept an arbitrary number of arguments. These features provide great flexibility when designing functions that need to handle a varying number of inputs.

Example:

Python
# *args exampledeffun(*args):returnsum(args)print(fun(1,2,3,4))print(fun(5,10,15))# **kwargs exampledeffun(**kwargs):fork,valinkwargs.items():print(k,val)fun(a=1,b=2,c=3)

Output
1030a 1b 2c 3

Let's explore *args and **kwargs in detail:

There are two special symbols to pass multiple arguments:

*args and **kwargs in Python
*args and **kwargs in Python

Special Symbols Used for passing arguments in Python:

  • *args (Non-Keyword Arguments)
  • **kwargs (Keyword Arguments)

Note: “We use the "wildcard" or "*" notation like this - *args OR **kwargs - as our function's argument when we have doubts about the number of  arguments we should pass in a function.” 

Python *args

The special syntax*args in function definitionsis used to pass a variable number of arguments to a function. It is used to pass a non-keyworded, variable-length argument list. These arguments aregrouped into a tuple, allowing the function to handle any number of inputs.

For example, a multiply() function can use *args to accept any number of inputs and multiply them. The *makes the variable iterable, so it can be looped through or used with functions like map() and filter().

Example 1:

Python program to illustrate *args for a variable number of arguments

python
defmyFun(*argv):forarginargv:print(arg)myFun('Hello','Welcome','to','GeeksforGeeks')

Output
HelloWelcometoGeeksforGeeks

Example 2:

Python program to illustrate *args with a first extra argument.

Python
deffun(arg1,*argv):print("First argument :",arg1)forarginargv:print("Argument *argv :",arg)fun('Hello','Welcome','to','GeeksforGeeks')

Output
First argument : HelloArgument *argv : WelcomeArgument *argv : toArgument *argv : GeeksforGeeks

Python **kwargs

The special syntax**kwargs in function definitions is used to pass a variable length argument list. We use the namekwargs with the double star **.

  • A keyword argument is where you provide a name to the variable as you pass it into the function.
  • It collects all the additional keyword arguments passed to the function andstores them in a dictionary.

Example 1: 

Python
deffun(**kwargs):fork,valinkwargs.items():print("%s ==%s"%(k,val))# Driver codefun(s1='Geeks',s2='for',s3='Geeks')

Output
s1 == Geekss2 == fors3 == Geeks

For s1='Geeks', s1 is key and 'Geeks' is a value. In simple words, what we assign is value and to whom we assign is key. 

Example 2:

Python
deffun(arg1,**kwargs):fork,valinkwargs.items():print("%s ==%s"%(k,val))# Driver codefun("Hi",s1='Geeks',s2='for',s3='Geeks')

Output
s1 == Geekss2 == fors3 == Geeks

Using both *args and **kwargs

We can use both *args and **kwargs in the same function to accept a mix of positional and keyword arguments.

Example:

Python
deffun(*args,**kwargs):print("Positional arguments:",args)print("Keyword arguments:",kwargs)fun(1,2,3,a=4,b=5)

Output
Positional arguments: (1, 2, 3)Keyword arguments: {'a': 4, 'b': 5}

In this example, the fun can handle both positional and keyword arguments. The args parameter collects positional arguments into a tuple, while the kwargs parameter collects keyword arguments into a dictionary.


Keyword Arguments in Python
Visit Courseexplore course icon
Improve
Article Tags :
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp