Keyword Arguments in Python – Syntax, Examples & Usage

Introduction

Python provides different ways to pass arguments to functions. One of the most useful methods is keyword arguments. Keyword arguments allow you to pass values by explicitly specifying the parameter names rather than relying solely on their position.

Using keyword arguments improves code readability, reduces mistakes, and makes function calls easier to understand. They are especially useful when functions have multiple parameters or optional values.

For example, when creating an employee record, using keyword arguments makes it immediately clear which value belongs to which parameter:


create_employee(name="John", age=25, department="IT")

Compared to positional arguments:


create_employee("John", 25, "IT")

The keyword version is much easier to read and maintain.

What are Keyword Arguments in Python?

Keyword arguments are arguments passed to a function using the parameter names.

Instead of relying on the order of values, you explicitly specify which parameter each value belongs to.

Example:


def student(name, age):
    print(name, age)
student(name="John", age=20)

Output:

John 20

In this example:

  • name=”John” is a keyword argument.
  • age=20 is a keyword argument.

Python matches values using parameter names instead of positions.

Why Use Keyword Arguments?

Keyword arguments offer several benefits:

  • Improve readability.
  • Reduce confusion.
  • Avoid errors caused by incorrect argument order.
  • Make function calls self-explanatory.
  • Work well with optional parameters.

Positional Argument Example


def employee(name, age, salary):
    print(name, age, salary)
employee("John", 25, 50000)

Keyword Argument Example


def employee(name, age, salary):
    print(name, age, salary)
employee(
    name="John",
    age=25,
    salary=50000
)

The second version is easier to understand.

Syntax

The basic syntax is:


function_name(
    parameter=value
)

Example:


def greet(name):
    print("Hello", name)
greet(name="Emma")

Output:

Hello Emma

How Keyword Arguments Work?

When Python encounters keyword arguments:

  1. It identifies parameter names.
  2. It assigns values based on those names.
  3. Argument order becomes less important.

Example:


def employee(name, age):
    print(name, age)
employee(age=30, name="David")

Output:

David 30

Notice that the order was changed, but the output remains correct.

Positional Arguments vs Keyword Arguments

Positional Arguments


def student(name, age):
    print(name, age)
student("John", 20)

Output:

John 20

Keyword Arguments


def student(name, age):
    print(name, age)
student(age=20, name="John")

Output:

John 20

Keyword arguments do not depend on position.

Reordering Arguments

One of the biggest advantages of keyword arguments is flexibility.

Example:


def product(name, price, quantity):
    print(name, price, quantity)

product(
    quantity=5,
    price=100,
    name="Keyboard"
)

Output:

Keyboard 100 5

Python correctly maps the values to their parameters.

Keyword Arguments with Default Arguments

Keyword arguments work very well with default arguments.

Example:


def greet(name="Guest"):
    print("Hello", name)
greet(name="Emma")

Output:

Hello Emma

Using the Default Value


def greet(name="Guest"):
    print("Hello", name)
greet()

Output:

Hello Guest

Mixing Positional and Keyword Arguments

Python allows mixing positional and keyword arguments.

Example:


def employee(name, age, salary):
    print(name, age, salary)
employee(
    "John",
    age=25,
    salary=50000
)

Output:

John 25 50000

This is perfectly valid.

Rules for Mixing Arguments

Positional arguments must come before keyword arguments.

Correct:


def employee(name, age):
    print(name, age)
employee(
    "John",
    age=25
)

Incorrect:


employee(
    age=25,
    "John"
)

Output:

SyntaxError

Python does not allow positional arguments after keyword arguments.

Using Keyword Arguments with Return Values

Example:


def add(a, b):
    return a + b
result = add(
    a=10,
    b=20
)
print(result)

Output:

30

Keyword arguments work exactly the same with functions that return values.

Keyword Arguments in Real-Life Programs

Keyword arguments are widely used in professional software development because they improve code clarity.

Real-Life Example: Student Registration System


def register_student(name, course, age):
    print(name, course, age)
register_student(
    name="Emma",
    course="Python",
    age=22
)

Output:

Emma Python 22

The purpose of each value is immediately clear.

Real-Life Examples:

1. Online Shopping Website


def order_product(product, quantity, price):
    print(product, quantity, price)
order_product(
    product="Laptop",
    quantity=2,
    price=50000
)

Output:

Laptop 2 50000

2. Banking Application


def transfer_money(
    sender,
    receiver,
    amount
):
    print(sender, receiver, amount)
transfer_money(
    sender="John",
    receiver="David",
    amount=1000
)

Output:

John David 1000

Financial applications often use keyword arguments for clarity.

3. Employee Management System


def add_employee(
    name,
    department,
    salary
):
    print(name, department, salary)
add_employee(
    salary=60000,
    department="IT",
    name="Emma"
)

Output:

Emma IT 60000

4. Email System


def send_email(
    recipient,
    subject,
    message
):
    print(recipient)

send_email(
    recipient="user@example.com",
    subject="Welcome",
    message="Hello User"
)

Output:

user@example.com

Advantages of Keyword Arguments

1. Improved Readability

Function calls become self-explanatory.


calculate_salary(
    hours=40,
    rate=20
)

2. Reduced Errors

Incorrect ordering is less likely.

3. Better Maintenance

Future developers can understand code easily.

4. Flexible Ordering

Arguments can be provided in any order.

5. Ideal for Functions with Many Parameters

Keyword arguments make large function calls manageable.

Common Mistakes

1. Misspelling Parameter Names

Incorrect:


def student(name, age):
    print(name)
student(nam="John", age=20)

Output:

TypeError

Correct:


student(name="John", age=20)

2. Using Positional Arguments After Keyword Arguments

Incorrect:


employee(
    age=25,
    "John"
)

Output:

SyntaxError

Correct:


employee(
    "John",
    age=25
)

3. Repeating the Same Argument

Incorrect:


def student(name):
    print(name)
student(
    "John",
    name="Emma"
)

Output:

TypeError

The parameter receives multiple values.

4. Omitting Required Parameters

Incorrect:


def employee(name, age):
    print(name, age)
employee(name="John")

Output:

TypeError

All required parameters must be provided.

5. Using Invalid Parameter Names

Incorrect:


student(
    username="John"
)

If the function expects name, Python raises an error.

Best Practices

1. Use Keyword Arguments for Readability

Prefer:


employee(
    name="John",
    age=25
)

Instead of:


employee(
    "John",
    25
)

For complex functions.

2. Use Meaningful Parameter Names


def calculate_salary(
    hours_worked,
    hourly_rate
):

Avoid vague names.

3. Combine with Default Arguments


def greet(
    name="Guest"
):

This provides flexibility.

4. Use Keyword Arguments in Long Function Calls

Large functions become easier to understand.

5. Follow Consistent Coding Style

Choose a clear style and use it consistently across your project.

Conclusion

Keyword arguments are an essential feature of Python functions that allow values to be passed using parameter names instead of relying solely on position. They improve code readability, reduce errors, and make function calls more flexible and self-documenting.

Whether you’re developing web applications, banking systems, e-commerce platforms, APIs, or automation scripts, keyword arguments help create cleaner and more maintainable code.

Related Python Tutorials