Contents

Python Interactive Guide - Step 3 Functions (3) - Special Parameters

Series - Python Interactive Guide
Info
  • 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.”

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.

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.

*args Parameter (Variable-Length Positional Arguments)
def function_name(*args): ...
  • The *args parameter receives an arbitrary number of positional arguments as a tuple and assigns it to the variable args
  • 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.

Using the unpacking operator (*), you can unpack lists and tuples and pass them as variable-length positional arguments to a function:

📚Practice Problem
  1. Create a function that takes any number of numerical values and returns both the average and maximum value of the inputs
  2. Call this function using the unpacking operator (*) with the following inputs and display the results:
    1. numbers = [1, 2, 3, 4, 5]
    2. Both numbers and numbers2 = (1000, 100, 10) combined
    3. Integers from 0 to 100
Example Solution

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 Parameter (Variable-Length Keyword Arguments)
def function_name(**kwargs): ...
  • **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:

Using the dictionary unpacking operator (**), you can unpack dictionaries to pass them as keyword arguments to a function:

📚Practice Problem
  1. 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
  2. Call this function with the following dictionaries using the dictionary unpacking operator (**) and display the results:

    1. scores1 = {"Alice": 85, "Bob": 58, "Charlie": 70}
    2. scores2 = {"David": 45, "Eve": 100}
    3. Both scores1 and scores2 combined
Example Solution
📚Practice Problem

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 are 10% and 20% discount coupons)
  • Return the total price after applying the highest discount rate coupon from the inputs
Example Solution

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.)

Positional-only Parameters
def function_name(positional_only_param, ..., /, regular_param, ...): ...

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.

Keyword-only Parameters
def function_name(regular_param, ..., *, keyword_only_param, ...): ...
📚Practice Problem

Create a function get_fullname that meets the following requirements:

  1. 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”
  2. first_name and last_name should be positional-only parameters
  3. separator should be a keyword-only parameter
  4. The default value for separator should be a space (" ")
Example Solution

Related Content