Skip to ContentGo to accessibility pageKeyboard shortcuts menu
OpenStax Logo

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
  2. name="Ash"
  3. msg="Hiya"
  4. 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
  2. name and count
  3. 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.

Citation/Attribution

This book may not be used in the training of large language models or otherwise be ingested into large language models or generative AI offerings without OpenStax's permission.

Want to cite, share, or modify this book? This book uses the Creative Commons Attribution License and you must attribute OpenStax.

Attribution information
  • If you are redistributing all or part of this book in a print format, then you must include on every physical page the following attribution:
    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction
  • If you are redistributing all or part of this book in a digital format, then you must include on every digital page view the following attribution:
    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction
Citation information

© Jul 30, 2024 OpenStax. Textbook content produced by OpenStax is licensed under a Creative Commons Attribution License . The OpenStax name, OpenStax logo, OpenStax book covers, OpenStax CNX name, and OpenStax CNX logo are not subject to the Creative Commons license and may not be reproduced without the prior and express written consent of Rice University.

This book utilizes the OpenStax Python Code Runner. The code runner is developed by Wiley and is All Rights Reserved.