Python Keyword Arguments

Summary: in this tutorial, you’ll learn about the Python keyword arguments, and how to use them to make function calls more obvious.

Introduction to the Python keyword arguments #

Let’s start with a simple function that calculates the net price from the selling price and discount:

def get_net_price(price, discount):
    return price * (1-discount)Code language: Python (python)

The get_net_price() function has two parameters: price and discount.

The following shows how to call the get_net_price() function to calculate the net price from the price 100 and discount 10%:

def get_net_price(price, discount):
    return price * (1-discount)

net_price = get_net_price(100, 0.1)
print(f'{net_price: .2f}')Code language: Python (python)

Try it

Output:

 90.00Code language: Python (python)

In this example, we use an f-string to format the output number with two decimal places (f'{net_price: .2f}').

In the get_net_price(100, 0.1) function call, we pass each argument as a positional argument. In other words, we pass the price argument first and the discount argument second.

However, the function call get_net_price(100, 0.1) has a readability issue. Because by looking at that function call only, you don’t know which argument is price and which one is the discount.

On top of that, when you call the get_net_price() function, you need to know the position of each argument.

If you don’t, the function will calculate the net_price incorrectly. For example:

def get_net_price(price, discount):
    return price * (1-discount)

net_price = get_net_price(0.1, 100)
print(f'{net_price: .2f}')Code language: Python (python)

Try it

Output:

-9.9Code language: Python (python)

To improve the readability, Python introduces keyword arguments.

The following shows the keyword argument syntax:

fn(parameter1=value1,parameter2=value2)Code language: Python (python)

By using the keyword argument syntax, you don’t need to specify the arguments in the same order as defined in the function.

Therefore, you can call a function by swapping the argument positions like this:

fn(parameter2=value2,parameter1=value1)Code language: Python (python)

The following shows how to use the keyword argument syntax to call the get_net_price() function:

def get_net_price(price, discount):
    return price * (1-discount)

net_price = get_net_price(
    price=100, 
    discount=0.1
)

print(f'{net_price: .2f}')Code language: Python (python)

Try it

Or:

def get_net_price(price, discount):
    return price * (1-discount)

net_price = get_net_price(
    discount=0.1,
    price=100
)

print(f'{net_price: .2f}')Code language: Python (python)

Try it

Both of them returns the same result.

It is possible to leave a trailing comma after the last argument:

def get_net_price(price, discount):
    return price * (1-discount)

net_price = get_net_price(
    discount=0.1,
    price=100,
)

print(f'{net_price: .2f}')Code language: PHP (php)

Try it

When you use the keyword arguments, their names that matter, not their positions.

def get_net_price(price, discount):
    return price * (1-discount)

net_price = get_net_price(
    price=100, 
    discount=0.1
)
print(net_price)Code language: PHP (php)

Try it

Note that you can call a function by mixing positional and keyword arguments. For example:

def get_net_price(price, discount):
    return price * (1-discount)

net_price = get_net_price(
    100, 
    discount=0.1
)

print(f'{net_price: .2f}')Code language: Python (python)

Try it

Keyword arguments and default parameters #

Suppose that you have the following get_net_price() function that calculates the net price from the selling price, tax, and discount.

def get_net_price(price, tax_rate=0.07, discount=0.05):
    discounted_price = price * (1 - discount)  
    net_price = discounted_price * (1 + tax_rate)  
    return net_priceCode language: Python (python)

In the get_net_price() function, the tax and discount parameters have default values of 7% and 5% respectively.

The following calls the get_net_price() function and uses the default values for tax and discount parameters:

def get_net_price(price, tax_rate=0.07, discount=0.05):
    discounted_price = price * (1 - discount)  
    net_price = discounted_price * (1 + tax_rate)  
    return net_price

net_price = get_net_price(100)
print(f'{net_price: .2f}')Code language: Python (python)

Try it

Output:

101.65Code language: Python (python)

Suppose that you want to use the default value for the tax parameter but not discount. The following function call doesn’t work correctly.

def get_net_price(price, tax_rate=0.07, discount=0.05):
    discounted_price = price * (1 - discount)  
    net_price = discounted_price * (1 + tax_rate)  
    return net_price

net_price = get_net_price(100, 0.06)
print(f'{net_price: .2f}')Code language: Python (python)

Try it

Output:

100.70Code language: CSS (css)

… because Python will assign 100 to price and 0.1 to tax, not discount.

To fix this, you must use keyword arguments:

def get_net_price(price, tax_rate=0.07, discount=0.05):
    discounted_price = price * (1 - discount)  
    net_price = discounted_price * (1 + tax_rate)  
    return net_price

net_price = get_net_price(
    price=100, 
    discount=0.06
)
print(f'{net_price: .2f}')Code language: Python (python)

Try it

Output:

100.58Code language: Shell Session (shell)

Or you can mix the positional and keyword arguments:

def get_net_price(price, tax_rate=0.07, discount=0.05):
    discounted_price = price * (1 - discount)  
    net_price = discounted_price * (1 + tax_rate)  
    return net_price

net_price = get_net_price(
    100, 
    discount=0.06
)
print(f'{net_price: .2f}')Code language: Python (python)

Try it

Output:

100.58Code language: Shell Session (shell)

Python keyword argument requirements #

Once you use a keyword argument, you need to use keyword arguments for the remaining parameters.

The following will result in an error because it uses the positional argument after a keyword argument:

def get_net_price(price, tax_rate=0.07, discount=0.05):
    discounted_price = price * (1 - discount)  
    net_price = discounted_price * (1 + tax_rate)  
    return net_price

net_price = get_net_price(
    100, 
    tax=0.08, 
    0.06
)

print(f'{net_price: .2f}')Code language: Python (python)

Try it

Error:

SyntaxError: positional argument follows keyword argumentCode language: Shell Session (shell)

To fix this, you need to use the keyword argument for the third argument like this:

def get_net_price(price, tax_rate=0.07, discount=0.05):
    discounted_price = price * (1 - discount)  
    net_price = discounted_price * (1 + tax_rate)  
    return net_price

net_price = get_net_price(
    100, 
    tax_rate=0.08, 
    discount=0.06
)

print(f'{net_price: .2f}')Code language: Python (python)

Try it

Output:

101.52Code language: plaintext (plaintext)

Summary #

  • Use the Python keyword arguments to make your function call more readable and obvious, especially for functions that accept many arguments.
  • All the arguments after the first keyword argument must also be keyword arguments too.

Quiz #

Was this tutorial helpful ?