Loading [MathJax]/jax/input/MathML/jax.js
Skip to main content
Library homepage
 

Text Color

Text Size

 

Margin Size

 

Font Type

Enable Dyslexic Font
Engineering LibreTexts

6.6: Keyword Arguments

( \newcommand{\kernel}{\mathrm{null}\,}\)

Learning Objectives

By the end of this section you should be able to

  • Describe the difference between positional and keyword arguments.
  • Create functions that use positional and keyword arguments and default parameter values.

Keyword arguments

So far, functions have been called using positional arguments, which are arguments that are assigned to parameters in order. Python also allows keyword arguments, which are arguments that use parameter names to assign values rather than order. When mixing positional and keyword arguments, positional arguments must come first in the correct order, before any keyword arguments.

Checkpoint: Using keyword arguments
Concepts in Practice: Using keyword and positional arguments

Consider the following function:

    def greeting(msg, name, count):
      i = 0
      for i in range(0, count):
        print(msg, name)
1.
Which is the positional argument in greeting(count=1, name="Ash", msg="Hiya")?
  1. count=1
  • name="Ash"
  • msg="Hiya"
  • None
  • 2.
    What is the output of greeting(count=2, name="Ash", msg="Hiya")?
    1. Ash Hiya
      Ash Hiya
    2. Hiya Ash
      Hiya Ash
    3.
    Which is the positional argument in greeting("Welcome", count=1, name="Anita")?
    1. "Welcome"
    2. count=1
    3. name="Anita"
    4.
    Which function call would produce an error?
    1. greeting("Morning", "Morgan", count=3)
    2. greeting(count=1,"Hi", "Bea")
    3. greeting("Cheers", "Colleagues", 10)

    Default parameter values

    Functions can define default parameter values to use if a positional or keyword argument is not provided for the parameter. Ex: def season(m, d, hemi="N"): defines a default value of "N" for the hemi parameter. Note: Default parameter values are only defined once to be used by the function, so mutable objects (such as lists) should not be used as default values.

    The physics example below calculates weight as a force in newtons given mass in kilograms and acceleration in ms2ms2. Gravity on Earth is 9.8 ms2ms2, and gravity on Mars is 3.7 ms2ms2.

    Checkpoint: Using default parameter values
    Concepts in Practice: Using default parameter values

    Consider the following updated version of greeting():

        def greeting(msg, name="Friend", count=1):
          i = 0
          for i in range(0, count):
            print(msg, name)
    5.
    Which parameters have default values?
    1. msg
  • name and count
  • all
  • 6.
    Which function call is correct?
    1. greeting()
    2. greeting(name="Gina")
    3. greeting("Greetings")
    7.
    What is the output of greeting(count=0, msg="Hello")?
    1. Hello Friend
    2. nothing
    3. Error
    PEP 8 recommendations: spacing

    The PEP 8 style guide recommends no spaces around = when indicating keyword arguments and default parameter values.

    Try It: Stream donations

    Write a function, donate(), that lets an online viewer send a donation to a streamer. donate() has three parameters:

    • amount: amount to donate, default value: 5
    • name: donor's name, default value: "Anonymous"
    • msg: donor's message, default value: ""

    Given:

        donate(10, "gg")

    The output is:

        Anonymous donated 10 credits: gg

    Write function calls that use the default values along with positional and keyword arguments.


    This page titled 6.6: Keyword Arguments is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by OpenStax via source content that was edited to the style and standards of the LibreTexts platform.

    Support Center

    How can we help?