Python programming
function arguments
keyword arguments
coding best practices
programming tips

Normal arguments vs. keyword arguments

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In Python, the difference between positional and keyword arguments is mostly about how values are matched to parameters. Positional arguments rely on order, while keyword arguments rely on parameter names, and understanding that distinction makes function calls easier to read and function signatures easier to design.

Positional Arguments

With positional arguments, Python assigns values based on their order in the call.

python
1def greet(name, greeting):
2    return f"{greeting}, {name}"
3
4print(greet("Maria", "Hello"))

Here, "Maria" goes to name and "Hello" goes to greeting because of position, not because of the variable names in the call.

Positional arguments are concise and natural when the meaning is obvious from the order.

Keyword Arguments

With keyword arguments, you name the target parameter explicitly.

python
1def greet(name, greeting):
2    return f"{greeting}, {name}"
3
4print(greet(name="Maria", greeting="Hello"))
5print(greet(greeting="Hello", name="Maria"))

Because the mapping is name-based, keyword arguments can appear in a different order and still work correctly.

They are especially useful when:

  • A function has several parameters of similar type
  • Some arguments are optional
  • Readability matters more than brevity

Mixing Positional and Keyword Arguments

Python lets you combine both styles, but positional arguments must come first.

python
1def connect(host, port, use_ssl=False):
2    return host, port, use_ssl
3
4print(connect("example.com", 443, use_ssl=True))

This is valid because the positional values come before the keyword argument.

This is invalid:

python
# connect(host="example.com", 443)

Once you start using keywords in a call, you cannot add a plain positional argument afterward.

Why Keyword Arguments Improve Clarity

Consider a function call such as:

python
send_email("[email protected]", "Build failed", True, 3)

Without reading the function signature, the True and 3 are not very informative. Keywords make the call self-documenting:

python
1send_email(
2    "[email protected]",
3    "Build failed",
4    urgent=True,
5    retry_count=3,
6)

That is one of the main reasons keyword arguments are common in APIs with many optional settings.

Positional-Only and Keyword-Only Parameters

Modern Python also lets function authors control how arguments must be passed.

python
def example(a, b, /, *, verbose=False):
    print(a, b, verbose)

In this signature:

  • 'a and b are positional-only because they appear before /'
  • 'verbose is keyword-only because it appears after *'

This is useful when designing stable APIs or preventing ambiguous calls.

Defaults Work Especially Well With Keywords

Keyword arguments become even more useful when a function has optional parameters with defaults.

python
1def create_user(name, active=True, admin=False):
2    return {"name": name, "active": active, "admin": admin}
3
4print(create_user("Ada"))
5print(create_user("Ada", admin=True))

The keyword form makes optional behavior obvious at the call site and avoids memorizing the exact position of every flag-like argument.

Common Pitfalls

  • Passing many positional arguments of the same type makes calls hard to read and easy to mix up.
  • Using the same parameter both positionally and by keyword causes TypeError.
  • Forgetting that positional arguments must come before keyword arguments leads to syntax errors.
  • Overusing keywords for tiny, obvious calls can make code noisy instead of clearer.

Library APIs often reserve keywords for the parameters where the meaning is least obvious, which is a useful design habit to copy. That improves call-site readability quickly.

Summary

  • Positional arguments are matched by order; keyword arguments are matched by parameter name.
  • Keyword arguments improve clarity when function calls have several optional or similar-looking values.
  • You can mix both styles, but positional arguments must come first.
  • Python also supports positional-only and keyword-only parameters when API design needs stricter rules.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.