Python Argument refers to information that you pass into a function. A function can have 0, set amount, or infinite arguments passed to it. We’ll run through an example of each below.
Note: You may hear the word parameter
get used. This is the same as argument and two can be used interchangeably.
def my_function(argument1, argument2): # Do something
Another interesting aspect of function arguments is that they can have a default value. This means if you do not specify them when you call a function, then their default value will be used.
In fact, I bet you use more python argument defaults than you do specified arguments.
def my_function(arg1, arg2="default value"): print (arg1, arg2) my_function(arg1="Bob") Returned: "Bob", "default value"
You can name arguments whatever you want! Just be careful that you aren’t using a word that is already in the python name space. You may get unintended results. Some of these are print
, abs
, for
, while
, etc.
Arguments are extremely common throughout the Python language and it’s libraries. In fact, it’s extremely popular as a part of fundamental coding languages in general.
Let’s take a look at a python argument code sample
Python Argument¶
Let's take a look at the different ways we can use arguments in python.
- Functions that take no arguments
- Functions that take required arguments
- Functions that have default values for arguments
- Functions that can take a variable amount of argument values
- Functions that can take a variable amount of arguments
Functions that take no arguments¶
def my_func():
print ("This function has no arguments")
my_func()
# Notice how no information is passed to the function
Functions that take required arguments¶
Functions with arguments that are required are those who do not have a default value. This will be required when the user is calling them.
def my_func_with_required(arg1, arg2):
print (arg1, arg2)
my_func_with_required()
Notice how I didn't specify arguments that were required, then python threw me an error. Let's try again but defining them this time.
my_func_with_required(arg1="Argument1", arg2="Argument2")
Functions that have default values for arguments¶
You can also set default values for arguments. This means that they won't be required to be defined by the user. You do this when you create the function.
def my_func_w_default(arg1, arg2="Default Value"):
print (arg1, arg2)
my_func_w_default(arg1="I'm setting this value")
Functions that can take a variable amount of argument values¶
A fun aspect of python is the ability to set a variable amount of argument values. You may not know how many argument you values will get, so you need a function that can handle the flexibility. This is done via the *args keyword.
Note that the * sign is known as the unpacking operator.
Notice below that we are passing 3 different values
def my_func_w_args(*args):
for x in args:
print (x)
my_func_w_args(1, 2, 3)
Functions that can take a variable amount of arguments¶
You can also do the same thing, but with named arguments
def my_func_w_kwargs(**kwargs):
for x in kwargs:
print (x)
print ()
for x in kwargs.values():
print (x)
my_func_w_kwargs(arg1="Bob", arg2="Fred", arg3="Katie", arg4="Sally")
Notice how you need to call .values() on your kwargs in this case to extract their values.
Check out more Python Vocabulary on our Glossary Page