Python Arguments
Learn all about Python Arguments in this comprehensive tutorial.
- •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:
Parameters vs Arguments
The terms parameter and argument can be used for the same thing: information that are passed into a function.
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.
If you try to call the function with the wrong number of arguments, you will get an error:
Default Parameter Values
You can assign default values to parameters. If the function is called without an argument, it uses the default value:
Keyword Arguments
You can send arguments with the key = value syntax.
This way, with keyword arguments, the order of the arguments does not matter.
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:
The order matters with positional arguments:
Mixing Positional and Keyword Arguments
You can mix positional and keyword arguments in a function call.
However, positional arguments must come before keyword arguments:
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:
Return Values
Functions can return values using the return statement:
Returning Different Data Types
Functions can return any data type, including lists, tuples, dictionaries, and more.
Positional-Only Arguments
You can specify that a function can have ONLY positional arguments.
To specify positional-only arguments, add , / after the arguments:
Without the , / you are actually allowed to use keyword arguments even if the function expects positional arguments:
With , /, you will get an error if you try to use keyword arguments:
Keyword-Only Arguments
To specify that a function can have only keyword arguments, add *, before the arguments:
Without *,, you are allowed to use positional arguments even if the function expects keyword arguments:
With *,, you will get an error if you try to use positional arguments:
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:
Module quiz
2 questionsWhich of the following is true about Python Arguments?
What is the most common pitfall when working with Python Arguments?
Answer all questions to submit.