Skip to ContentGo to accessibility page

6.6 Keyword arguments

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

Using keyword arguments; ch 6, video 13

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

Using default parameter values; ch 6, video 14

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
Reuse and redistribution of this content in digital or print format:
  • 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 prior written permission.
  • This book uses the Creative Commons Attribution-NonCommercial-ShareAlike License, which means that you can reuse and modify the material only for noncommercial purposes, must attribute OpenStax, and must distribute any derivative works under the same license.
  • Any commercial printing of this textbook, including using a local or custom printer, must be approved by OpenStax, and proper citation provided.
  • OpenStax-copyrighted images, activities, assessments, and similar components of this book are subject to the same licensing – CC-BY-NC-SA. They can be used for noncommercial purposes with attribution. Commercial use requires permission.
  • Permission requests: Anyone who intends to incorporate this content (including text, images, and other components) into large language models, use it in AI offerings, use it commercially (including in print), and/or has questions about another use case is welcome to complete our reuse request form.
Attribution information
  • If you are redistributing all or part of this book in a noncommercial 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 noncommercial digital format, then for every page that includes OpenStax content, you must license the derivative work under the same CC-BY-NC-SA license as the original, and 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

The information below includes the information needed to generate citations in most major styles (APA, MLA, etc.); you must reformat and organize the information as needed to fit the requirements of the style. Use the information below to generate a citation. We recommend using a citation tool such as this one.

© Apr 23, 2026 OpenStax. Textbook content produced by OpenStax is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike License. The OpenStax name, OpenStax logo, OpenStax book covers, OpenStax CNX name, and OpenStax CNX logo, and Rice University name, and Rice University logo trademarks, or wordmarks 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.