An argument is a value that is passed within a function when it is called.They are independent items, or variables, that contain data or codes. During the time of call each argument is always assigned to the parameter in the function definition.
Example: Simple argument
Python3
def GFG(name, num):
print ( "Hello from " , name + ', ' + num)
GFG( "geeks for geeks" , "25" )
|
Output:
Hello from geeks for geeks, 25
Calling the above code with no arguments or just one argument generates an error.
Variable Function Arguments
As shown above, functions had a fixed number of arguments. In Python, there are other ways to define a function that can take the variable number of arguments.
Different forms are discussed below:
- Python Default Arguments: Function arguments can have default values in Python. We provide a default value to an argument by using the assignment operator (=).
Example:
Python3
def GFG(name, num = "25" ):
print ( "Hello from" , name + ', ' + num)
GFG( "gfg" )
GFG( "gfg" , "26" )
|
Output:
Hello from gfg, 25
Hello from gfg, 26
Python3
def GFG(name, num):
print ( "hello from %s , %s" % (name, num))
GFG( "gfg" , "25" )
|
Output:
hello from gfg , 25
Python3
def GFG(name, num):
print ( "hello from %(n)s , %(s)s" % { 'n' : name, 's' : num})
GFG( "gfg" , "25" )
|
Output:
hello from gfg , 25
- Using new-style string formatting with number
Python3
def GFG(name, num):
print ( "hello from {0} , {1}" . format (name, num))
GFG( "gfg" , "25" )
|
Output:
hello from gfg , 25
- Using new-style string formatting with explicit names
Python3
def GFG(name, num):
print ( "hello from {n} , {r}" . format (n = name, r = num))
GFG( "gfg" , "25" )
|
Output:
hello from gfg , 25
Python3
def GFG(name, num):
print ( "hello from " + str (name) + " , " + str (num))
GFG( "gfg" , "25" )
|
Output:
hello from gfg , 25
- Using the new f-string formatting in Python 3.6
Python3
def GFG(name, num):
print (f 'hello from {name} , {num}' )
GFG( "gfg" , "25" )
|
Output:
hello from gfg , 25
Python3
def GFG( * args):
for info in args:
print (info)
GFG([ "Hello from" , "geeks" , 25 ], [ "Hello" , "gfg" , 26 ])
|
Output:
[‘Hello from’, ‘geeks’, 25]
[‘Hello’, ‘gfg’, 26]
Python3
def GFG( * * kwargs):
for key, value in kwargs.items():
print (key, value)
GFG(name = "geeks" , n = "- 25" )
GFG(name = "best" , n = "- 26" )
|
Output:
name geeks
n – 25
name best
n – 26
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
29 Dec, 2020
Like Article
Save Article