Python Argument

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.

  1. Functions that take no arguments
  2. Functions that take required arguments
  3. Functions that have default values for arguments
  4. Functions that can take a variable amount of argument values
  5. Functions that can take a variable amount of arguments

Functions that take no arguments

In [1]:
def my_func():
    print ("This function has no arguments")

my_func()

# Notice how no information is passed to the function
This function has no arguments

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.

In [2]:
def my_func_with_required(arg1, arg2):
    print (arg1, arg2)
    
my_func_with_required()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-ba8c8cedf5b8> in <module>
      2     print (arg1, arg2)
      3 
----> 4 my_func_with_required()

TypeError: my_func_with_required() missing 2 required positional arguments: 'arg1' and 'arg2'

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.

In [3]:
my_func_with_required(arg1="Argument1", arg2="Argument2")
Argument1 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.

In [4]:
def my_func_w_default(arg1, arg2="Default Value"):
    print (arg1, arg2)

my_func_w_default(arg1="I'm setting this value")
I'm setting this value Default 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

In [5]:
def my_func_w_args(*args):
    for x in args:
        print (x)
        
my_func_w_args(1, 2, 3)
1
2
3

Functions that can take a variable amount of arguments

You can also do the same thing, but with named arguments

In [6]:
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")
arg1
arg2
arg3
arg4

Bob
Fred
Katie
Sally

Notice how you need to call .values() on your kwargs in this case to extract their values.

Link to code above

Check out more Python Vocabulary on our Glossary Page