Python Tutorials · Python Functions

Python Arguments

Learn all about Python Arguments in this comprehensive tutorial.

5 min read beginner
  • Information can be passed into functions as arguments.
  • The terms parameter and argument can be used for the same thing: information that are passed into a function.
  • By default, a function must be called with the correct number of arguments.
  • You can assign default values to parameters.
  • You can send arguments with the key = value syntax.
  • When you call a function with arguments without using keywords, they are called positional arguments.
  • You can mix positional and keyword arguments in a function call.
  • You can send any data type as an argument to a function (string, number, list, dictionary, etc.
  • Functions can return values using the return statement:
  • Functions can return any data type, including lists, tuples, dictionaries, and more.
  • You can specify that a function can have ONLY positional arguments.
  • To specify that a function can have only keyword arguments, add *, before the arguments:
  • You can combine both argument types in the same function.

Arguments

Information can be passed into functions as arguments.

Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma.

The following example has a function with one argument (fname). When the function is called, we pass along a first name, which is used inside the function to print the full name:

python

Parameters vs Arguments

The terms parameter and argument can be used for the same thing: information that are passed into a function.

Note: From a function's perspective: A parameter is the variable listed inside the parentheses in the function definition. An argument is the actual value that is sent to the function when it is called.
python

Number of Arguments

By default, a function must be called with the correct number of arguments.

If your function expects 2 arguments, you must call it with exactly 2 arguments.

python

If you try to call the function with the wrong number of arguments, you will get an error:

python

Default Parameter Values

You can assign default values to parameters. If the function is called without an argument, it uses the default value:

python
python

Keyword Arguments

You can send arguments with the key = value syntax.

python

This way, with keyword arguments, the order of the arguments does not matter.

python
Note: The phrase Keyword Arguments is often shortened to kwargs in Python documentation.

Positional Arguments

When you call a function with arguments without using keywords, they are called positional arguments.

Positional arguments must be in the correct order:

python

The order matters with positional arguments:

python

Mixing Positional and Keyword Arguments

You can mix positional and keyword arguments in a function call.

However, positional arguments must come before keyword arguments:

python

Passing Different Data Types

You can send any data type as an argument to a function (string, number, list, dictionary, etc.).

The data type will be preserved inside the function:

python
python

Return Values

Functions can return values using the return statement:

python

Returning Different Data Types

Functions can return any data type, including lists, tuples, dictionaries, and more.

python
python

Positional-Only Arguments

You can specify that a function can have ONLY positional arguments.

To specify positional-only arguments, add , / after the arguments:

python

Without the , / you are actually allowed to use keyword arguments even if the function expects positional arguments:

python

With , /, you will get an error if you try to use keyword arguments:

python

Keyword-Only Arguments

To specify that a function can have only keyword arguments, add *, before the arguments:

python

Without *,, you are allowed to use positional arguments even if the function expects keyword arguments:

python

With *,, you will get an error if you try to use positional arguments:

python

Combining Positional-Only and Keyword-Only

You can combine both argument types in the same function.

Arguments before / are positional-only, and arguments after * are keyword-only:

python

Module quiz

2 questions
1

Which of the following is true about Python Arguments?

2

What is the most common pitfall when working with Python Arguments?

Answer all questions to submit.