Python Keyword Arguments

In the last lesson, we learned about Python Default Parameters. Now let’s pass values by parameter name so our function calls are easier to read.

βš™οΈ Let’s Talk About Calling Functions by Name

We already know how to call a function. We put the values in parentheses, in order. That is called positional arguments.

def introduce(name, age, city):
print(f"My name is {name}. I am {age} years old. I live in {city}.")
introduce("Alex", 25, "London")

This works. But we have to remember the order. Was it name, age, city? Or name, city, age? If we get it wrong, the output will be wrong.

That is where keyword arguments come in.

🧩 What Are Keyword Arguments in Python?

Keyword arguments let us specify which parameter gets which value. We write the parameter name, then an equals sign, then the value.

introduce(name="Alex", age=25, city="London")

Now the order does not matter. We can write them in any order.

introduce(city="London", name="Alex", age=25)

Same output. Because Python matches by name, not by position.

πŸ’‘ Why Use Keyword Arguments in Python?

There are three big reasons.

First, clarity. When we read introduce(name="Alex", age=25, city="London"), we immediately know what each value means. With positional arguments, we have to remember the order.

Second, we can skip parameters that have defaults. We learned about default parameters in the last article. If a parameter has a default, we do not have to provide it. But with positional arguments, we cannot skip one in the middle. We have to provide all of them in order.

Third, order does not matter. We can put them in whatever order makes sense to us. That is convenient.

πŸ§ͺ Mixing Positional and Keyword Arguments in Python

We can mix them. But there is a rule: positional arguments must come first. Keyword arguments come after.

This is correct:

introduce("Alex", age=25, city="London")

Here, "Alex" is positional and goes to name. The others are keyword.

This is wrong:

introduce(name="Alex", 25, "London") # Error!

We cannot put positional arguments after keyword arguments. Python will not know what to do.

So let’s remember: positional first, then keyword.

πŸ“Œ Python Keyword Arguments: A Real-Life Example

Let’s write a function that creates a user profile. It takes a username, email, and some optional settings.

def create_profile(username, email, is_admin=False, is_active=True):
print(f"Username: {username}")
print(f"Email: {email}")
print(f"Admin: {is_admin}")
print(f"Active: {is_active}")
print("-" * 20)
create_profile("alex123", "alex@example.com")

Output

Username: alex123
Email: alex@example.com
Admin: False
Active: True
--------------------

Now if we want to make an admin, we can use a keyword argument.

create_profile("sam456", "sam@example.com", is_admin=True)

Output

Username: sam456
Email: sam@example.com
Admin: True
Active: True
--------------------

We only changed what we needed. The rest used their defaults. That is the power of keyword arguments.

πŸ“₯ Keyword Arguments with Default Parameters in Python

Keyword arguments are especially useful when we have default parameters. Because we can skip the ones we do not want to change.

def order(item, quantity=1, size="medium", extra_cheese=False):
print(f"{quantity} {size} {item}(s)")
if extra_cheese:
print("With extra cheese")
order("pizza")
order("pizza", extra_cheese=True)
order("pizza", size="large", quantity=2)

Output

1 medium pizza(s)
1 medium pizza(s)
With extra cheese
2 large pizza(s)

See how we only specified what we wanted to change? That is much cleaner than passing all arguments positionally.

πŸ” Keyword Arguments in Built-in Functions

We can use keyword arguments with any function. Even built-in functions.

For example, the print function has a sep parameter and an end parameter. We usually use them as keyword arguments.

print("Hello", "World", sep=", ")
print("Hello", end=" ")
print("World")

Output

Hello, World
Hello World

That is keyword arguments in action.

⚠️ Common Mistakes with Python Keyword Arguments

❌ Mistake 1: Using a Wrong Parameter Name

introduce(nam="Alex", age=25, city="London") # Error!

Python will give us a TypeError because nam is not a parameter. It is a typo. We must use the exact parameter name.

So what do we do? We use the correct name.

introduce(name="Alex", age=25, city="London")

🚫 Mistake 2: Positional After Keyword

introduce(name="Alex", 25, "London") # Error!

So what do we do? We put positional arguments first.

introduce("Alex", age=25, city="London")

Or we use all keyword:

introduce(name="Alex", age=25, city="London")

πŸ›‘ Mistake 3: Using a Keyword Argument for a Parameter That Does Not Exist

introduce(name="Alex", age=25, country="UK") # Error!

Because country is not a parameter. It is city.

So what do we do? We use the correct parameter name.

introduce(name="Alex", age=25, city="UK")

Next, we will learn about Python Variable-Length Arguments and accept a flexible number of values.