Python Interactive Guide - Step 3 Functions (3) - Special Parameters
- This course, Python Interactive Guide, is designed to help you learn the basics of Python programming through hands-on, interactive examples.
- The “Style Guide” sections introduce clean coding practices, mainly based on PEP8.
- You can run and experiment with every code example.
Feel free to try things out - reloading the page will reset everything.
This is a continuation of “Step 3 Functions.”
3.2. Special Parameters
In the previous sections, we learned the basics of function parameters and arguments.
In this section, we’ll introduce special parameter forms that enable more flexible function definitions.
3.2.1. *args
Parameter (Variable-Length Positional Arguments)
When the number of arguments to pass to a function isn’t predetermined, you can use the unpacking operator (*
) introduced in Unpacking to accept any number of positional arguments.
- The
*args
parameter receives an arbitrary number of positional arguments as a tuple and assigns it to the variableargs
- By convention, the name
args
is used, but you can use a different name if you prefer
You can also assign the first few arguments to individual variables and the rest to args
:
All parameters defined after the *args
parameter cannot be assigned values as positional arguments, and can only be assigned as keyword arguments, becoming keyword-only parameters, which we’ll discuss later.
Unpacking Lists and Tuples
Using the unpacking operator (*
), you can unpack lists and tuples and pass them as variable-length positional arguments to a function:
- Create a function that takes any number of numerical values and returns both the average and maximum value of the inputs
- Call this function using the unpacking operator (
*
) with the following inputs and display the results:numbers = [1, 2, 3, 4, 5]
- Both
numbers
andnumbers2 = (1000, 100, 10)
combined - Integers from 0 to 100
Example Solution
3.2.2. **kwargs
Parameter (Variable-Length Keyword Arguments)
When the number of keyword arguments to pass to a function isn’t predetermined, you can use the dictionary unpacking operator (**
) to accept any number of keyword arguments.
**kwargs
receives any number of keyword arguments as a dictionary (dict
)- By convention, the name
kwargs
(short for “keyword arguments”) is used, but you can use a different name if you prefer
You can also combine this with regular arguments and variable-length positional arguments:
Unpacking Dictionaries
Using the dictionary unpacking operator (**
), you can unpack dictionaries to pass them as keyword arguments to a function:
-
Create a function that takes grade data as keyword arguments (name=score format) and returns a dictionary with the following information:
- Average score
- Highest score
- List of students with the highest score
-
Call this function with the following dictionaries using the dictionary unpacking operator (
**
) and display the results:scores1 = {"Alice": 85, "Bob": 58, "Charlie": 70}
scores2 = {"David": 45, "Eve": 100}
- Both
scores1
andscores2
combined
Example Solution
You are creating a checkout system for an online shopping site.
Create a function calculate_total
that takes any number of product prices as positional arguments and any number of discount coupons as keyword arguments, and calculates the total price.
- Usage example:
calculate_total(100, 50, 30, coupon1=0.1, coupon2=0.2)
(Cart has items priced$100
,$50
,$30
, and there are10%
and20%
discount coupons) - Return the total price after applying the highest discount rate coupon from the inputs
Example Solution
3.2.3. Positional-only Parameters (/
)
By adding /
to a function’s parameter list, all parameters before it can only accept positional arguments and cannot be assigned using keyword arguments.
Parameters that can only accept positional arguments are called positional-only parameters.
(This feature was introduced in Python 3.8.)
3.2.4. Keyword-only Parameters (*
)
By adding *
to a function’s parameter list, all parameters after it can only accept keyword arguments and cannot be assigned using positional arguments.
Parameters that can only accept keyword arguments are called keyword-only parameters.
Create a function get_fullname
that meets the following requirements:
- It should take a first name (
first_name
), last name (last_name
), and separator (separator
), and return a string with the first and last name separated by the separator- Example:
first_name
: “John”,last_name
: “Smith”,separator
: “##” => “John##Smith”
- Example:
first_name
andlast_name
should be positional-only parametersseparator
should be a keyword-only parameter- The default value for
separator
should be a space (" "
)