Introduction to Python Programming

# 6.6Keyword 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.

## 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$.

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

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.