Python Keyword Arguments

In the last lesson you learned about Default Parameters. There a function can fill in a value when the caller leaves it out. Now we look at the other side of the call. How do you pass values in so the call is easy to read? That is what keyword arguments are for.

🤔 Why Keyword Arguments?

Say you have a function that makes a user. It takes a name, an age, and a city.

def create_user(name, age, city):
print(f"{name}, {age}, lives in {city}")
create_user("Riya", 25, "Mumbai")

That call works. But read it again. Six months later you come back and see create_user("Riya", 25, "Mumbai"). Which one is the age? Is it the number? Or is “Mumbai” somehow the age? You have to scroll back up to the function to be sure.

Here is the real pain. When you pass values only by position, the call tells you what but never why. The reader has to remember the exact order of every parameter.

Keyword arguments fix that. You pass each value by its name, like age=25. Now the call explains itself right where you read it.

🧩 What Is a Keyword Argument?

A keyword argument is a value you pass to a function using the parameter’s name, in the form name=value. Python then matches the value to the right parameter by that name. It does not use where the value sits in the call.

Think of ordering food online. A positional order would be like saying “one, large, extra cheese” and trusting the kitchen to know which is which. A keyword order is “size=large, topping=extra cheese, quantity=one”. Same items, but now nobody can mix them up.

Here is the same create_user call written with keyword arguments.

def create_user(name, age, city):
print(f"{name}, {age}, lives in {city}")
create_user(name="Riya", age=25, city="Mumbai")

Run it and you get:

Output

Riya, 25, lives in Mumbai

The output is exactly the same as before. But the call now reads like a sentence. You can see at a glance that the age is 25 and the city is Mumbai. No scrolling back up.

🔀 Order Does Not Matter

When you use keyword arguments, the order you write them in does not matter at all. Python lines them up by name. So you can put them in any order you like.

This call passes the same three values, just shuffled around.

def create_user(name, age, city):
print(f"{name}, {age}, lives in {city}")
create_user(age=25, city="Mumbai", name="Riya")

Output

Riya, 25, lives in Mumbai

See? Even though age came first and name came last in the call, the result is identical. Python read the names, not the positions. With plain positional arguments this would have been a mess. The 25 would have landed in name.

Tip

This is great when a function has a few values that are easy to swap by accident, like two numbers or two strings. Naming them makes the mix-up impossible.

⚖️ Positional vs Keyword

Here is a quick side-by-side of the two ways to pass the same values.

Style The call Matched by
Positional create_user("Riya", 25, "Mumbai") Position (order)
Keyword create_user(name="Riya", age=25, city="Mumbai") Name

Both calls do the same job. The positional one is shorter. The keyword one is clearer. For short, obvious calls like print("hello"), positional is fine. The moment a call has several values, or a couple of bare numbers, keyword arguments pay off.

🔗 Mixing Positional and Keyword

You do not have to pick one style for the whole call. You can mix them. The one rule to remember is simple. Positional arguments must come first, then keyword arguments.

This is allowed, because the positional value comes before the keyword ones.

def create_user(name, age, city):
print(f"{name}, {age}, lives in {city}")
create_user("Riya", age=25, city="Mumbai")

Output

Riya, 25, lives in Mumbai

Here "Riya" is passed by position, so it fills name. Then age and city are passed by name. A common habit is to pass the obvious first value by position and name the rest for clarity.

But if you put a positional value after a keyword one, Python stops you.

# ❌ Avoid: positional argument after a keyword argument
create_user(name="Riya", 25, "Mumbai")
# ✅ Good: keep positional ones first
create_user("Riya", age=25, city="Mumbai")

Running the wrong version raises an error before anything prints:

Output

SyntaxError: positional argument follows keyword argument

The reason is simple. Once you name a value, Python can no longer tell which slot a later bare value should fill. So it asks you to keep all the positional ones up front.

🧰 A More Real Example

Keyword arguments shine when a function takes several settings and some of them have defaults. Imagine sending a message in a chat app.

def send_message(to, text, urgent=False, notify=True):
flag = "URGENT" if urgent else "normal"
print(f"To {to}: {text} ({flag}, notify={notify})")
send_message("Arjun", "Are we still on for 6pm?")
send_message("Alex", "Server is down!", urgent=True)
send_message(to="Riya", text="Happy birthday!", notify=False)

The output for these three calls is:

Output

To Arjun: Are we still on for 6pm? (normal, notify=True)
To Alex: Server is down! (URGENT, notify=True)
To Riya: Happy birthday! (normal, notify=False)

Look at the second call. It passes urgent=True by name and skips notify entirely. So notify keeps its default. Without keyword arguments you would have to pass every value in order just to reach urgent. Naming the one you care about lets you jump straight to it and leave the rest alone.

⚠️ Common Mistakes

A few slips trip people up early on. Watch out for these.

  • Putting a positional argument after a keyword one. Python raises SyntaxError: positional argument follows keyword argument. Keep positional values first.
  • Misspelling the parameter name. create_user(naem="Riya", ...) fails with a TypeError about an unexpected keyword argument, because Python looks for a parameter actually called naem.
  • Using the wrong name on purpose. The keyword must match the parameter name in the def line, not the variable you happen to have. The name in the call is the parameter’s name.

This shows the spelling trap and the fix.

# ❌ Avoid: the parameter is "name", not "naem"
create_user(naem="Riya", age=25, city="Mumbai")
# ✅ Good: match the parameter name exactly
create_user(name="Riya", age=25, city="Mumbai")

✅ Best Practices

Small habits that keep your calls clean.

  • Use keyword arguments when a call has several values, or any bare numbers and booleans like True or 0, so the reader knows what each one means.
  • Pass the first obvious value by position if you like, then name the rest. send_message("Alex", text="Hi") reads well.
  • For booleans especially, always name them. urgent=True tells you far more than a lone True sitting in a call.
  • Keep keyword names spelled exactly as the parameters in the function, so Python can match them.

🧩 What You’ve Learned

A quick recap of what you can now do.

  • ✅ Pass values to a function by name using the name=value form.
  • ✅ Reorder keyword arguments freely, because Python matches them by name, not position.
  • ✅ Mix positional and keyword arguments, keeping the positional ones first.
  • ✅ Read a call clearly without scrolling back to the function definition.
  • ✅ Spot the common errors: positional after keyword, and misspelled parameter names.

Check Your Knowledge

Test what you learned. Pick an answer for each question, then click Check.

  1. 1

    What is a keyword argument?

    Why: A keyword argument passes a value using the parameter's name in the form name=value.

  2. 2

    Given def create_user(name, age, city), what does create_user(age=25, city='Mumbai', name='Riya') do?

    Why: With keyword arguments Python matches by name, so the order you write them in does not matter.

  3. 3

    Which call is valid when mixing positional and keyword arguments?

    Why: Positional arguments must come first, then keyword arguments, so 'Riya' by position followed by named ones is valid.

  4. 4

    What happens if you write a positional argument after a keyword argument?

    Why: Python raises 'SyntaxError: positional argument follows keyword argument' before anything runs.

🚀 What’s Next?

So far each call has passed a fixed set of named values. But what if a function needs to accept any number of arguments, however many the caller sends? That is where you head next.

Variable Length Arguments

Share & Connect