Skip to main content
Engineering LibreTexts

6.6: Keyword Arguments

  • Page ID
    117561
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\dsum}{\displaystyle\sum\limits} \)

    \( \newcommand{\dint}{\displaystyle\int\limits} \)

    \( \newcommand{\dlim}{\displaystyle\lim\limits} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

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

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

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

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \(\newcommand{\longvect}{\overrightarrow}\)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)
    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)
    
    Concepts in Practice \(\PageIndex{1}\): Using keyword and positional arguments

    Which is the positional argument in greeting(count=1, name="Ash", msg="Hiya")?

    1. count=1
    2. name="Ash"
    3. msg="Hiya"
    4. None
    Answer

    d. The function call uses all keyword arguments to assign values to the named parameters.

    Concepts in Practice \(\PageIndex{2}\): Using keyword and positional arguments

    What is the output of greeting(count=2, name="Ash", msg="Hiya")?

    1. Ash Hiya
      Ash Hiya
    2. Hiya Ash
      Hiya Ash
    Answer

    b. "Hiya" is the value of msg and "Ash" is the value of name in the function.

    Concepts in Practice \(\PageIndex{3}\): Using keyword and positional arguments

    Which is the positional argument in greeting("Welcome", count=1, name="Anita")?

    1. "Welcome"
    2. count=1
    3. name="Anita"
    Answer

    a. "Welcome" is unnamed and assigned correctly to count. Positional arguments must come before keyword arguments.

    Concepts in Practice \(\PageIndex{4}\): Using keyword and positional arguments

    Which function call would produce an error?

    1. greeting("Morning", "Morgan", count=3)
    2. greeting(count=1,"Hi", "Bea")
    3. greeting("Cheers", "Colleagues", 10)
    Answer

    b. The positional arguments, "Hi" and "Bea" , follow the keyword argument count=1, which produces an error.

    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 \(\text{m}/\text{ s}^ 2\). Gravity on Earth is 9.8 ms2ms2, and gravity on Mars is 3.7 \(\text{m}/\text{s}^2\).

    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)
    
    Concepts in Practice \(\PageIndex{5}\): Using default parameter values

    Which parameters have default values?

    1. msg
    2. name and count
    3. all
    Answer

    b. name's default value is "Friend" , and count's default value is 1 . msg does not have default value.

    Concepts in Practice \(\PageIndex{6}\): Using default parameter values

    Which function call is correct?

    1. greeting()
    2. greeting(name="Gina")
    3. greeting("Greetings")
    Answer

    c. msg is a required argument assigned with "Greetings" . name and count use the default values. "Greetings Friend" is printed.

    Concepts in Practice \(\PageIndex{7}\): Using default parameter values

    What is the output of greeting(count=0, msg="Hello")?

    1. Hello Friend
    2. nothing
    3. Error
    Answer

    b. msg="Hello", name="Friend", and count=0, so the call is valid. The function executes but doesn't enter the for loop because count=0.

    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.

    Interactive Code
     
     
    Answer

    def donate(amount=5, name="Anonymous", msg=""):
    print(name, "donated", amount, "credits:", msg)

    donate()
    donate(msg="great stream", name="Tim")
    donate(10, "user123", "good luck")


    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.