Default and Keyword Arguments Explained

Default arguments and keyword arguments make Python functions easier to call.

They help you:

  • make some function inputs optional
  • write clearer function calls
  • avoid passing every value every time

This page explains what they are, how they work, and the main rules beginners should remember. If you are new to functions, start with Python functions explained or function parameters and arguments in Python.

Quick example

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

greet("Sam")
greet("Sam", message="Hi")
greet(name="Sam", message="Welcome")

Output:

Hello Sam
Hi Sam
Welcome Sam

In this example:

  • message="Hello" is a default argument
  • message="Hi" and name="Sam" in the function call are keyword arguments
  • all three function calls are valid

What this page teaches

  • What default arguments mean
  • What keyword arguments mean
  • How they make functions easier to call
  • How to avoid common mistakes when calling functions

What is a default argument?

A default argument is a parameter that already has a value.

If you do not pass that argument when calling the function, Python uses the default value instead.

This makes the parameter optional.

Example:

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

Here:

  • name is required
  • message is optional
  • if no message is given, Python uses "Hello"

Simple default argument example

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

greet("Sam")
greet("Sam", "Hi")

Output:

Hello Sam
Hi Sam

What happens here:

  • greet("Sam") uses the default value "Hello"
  • greet("Sam", "Hi") replaces the default with "Hi"

Default arguments are useful when a function usually uses the same value, but sometimes needs a different one.

What is a keyword argument?

A keyword argument is an argument passed using the parameter name.

Example:

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

greet(name="Sam", message="Hi")

Output:

Hi Sam

Here, Python matches values by parameter name:

  • name="Sam" goes to name
  • message="Hi" goes to message

Keyword arguments are helpful because they:

  • make code easier to read
  • show clearly what each value means
  • help when a function has several parameters

Positional arguments vs keyword arguments

Positional arguments

Positional arguments are matched by order.

def describe_pet(animal, name):
    print(name, "is a", animal)

describe_pet("cat", "Luna")

Output:

Luna is a cat

Python matches:

  • "cat" to animal
  • "Luna" to name

Keyword arguments

Keyword arguments are matched by parameter name.

def describe_pet(animal, name):
    print(name, "is a", animal)

describe_pet(name="Luna", animal="cat")

Output:

Luna is a cat

This time, the order does not matter because the names are included.

Beginners should learn both styles because many Python functions allow both.

Using default arguments with keyword arguments

Default arguments and keyword arguments often work together.

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

greet("Sam")
greet("Sam", message="Hi")
greet(name="Sam", message="Welcome")

Output:

Hello Sam
Hi Sam
Welcome Sam

What each call does:

  • greet("Sam")
    Uses the default value for message
  • greet("Sam", message="Hi")
    Overrides the default value with a keyword argument
  • greet(name="Sam", message="Welcome")
    Passes both values by keyword

Using keywords is often clearer when you want to override an optional value.

Rules beginners should remember

1. Required parameters come before default parameters

This is valid:

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

This is not valid:

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

Python will raise a syntax error because a required parameter cannot come after a default parameter.

2. You cannot place a non-default parameter after a default parameter

This is the same rule in another form:

  • valid: def func(a, b=10):
  • invalid: def func(a=10, b):

3. Positional arguments must come before keyword arguments in a call

Valid:

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

greet("Sam", message="Hi")

Invalid:

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

# greet(name="Sam", "Hi")

The invalid call is wrong because a positional argument comes after a keyword argument.

4. Do not pass the same argument twice

Invalid:

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

# greet("Sam", name="Alex")

Here, name gets a value twice:

  • "Sam" by position
  • "Alex" by keyword

That causes a TypeError.

Common errors and confusing cases

A lot of problems with arguments lead to TypeError. If you want a deeper explanation, see TypeError: missing required positional argument and TypeError: unexpected keyword argument.

Missing a required argument

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

# greet()

Why it fails:

  • name is required
  • no value was passed for name

Passing the same parameter twice

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

# greet("Sam", name="Alex")

Why it fails:

  • name already got "Sam" as the first positional argument
  • then name="Alex" tries to assign it again

Python reports something like:

TypeError: greet() got multiple values for argument 'name'

Using an unexpected keyword name

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

# greet(username="Sam")

Why it fails:

  • the function has name
  • it does not have username

Python reports something like:

TypeError: greet() got an unexpected keyword argument 'username'

Mixing positional and keyword arguments in the wrong order

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

# greet(name="Sam", "Hi")

Why it fails:

  • positional arguments must come first
  • keyword arguments come after them

Beginner tip: when to use each style

Use positional arguments when:

  • the function call is short
  • the meaning is obvious
  • the order is easy to remember

Example:

print("Hello")

Use keyword arguments when:

  • the function has several parameters
  • the meaning of each value is not obvious
  • you want the call to be easier to read

Example:

def create_user(name, age, active=True):
    print(name, age, active)

create_user(name="Sam", age=20, active=False)

Use default values when:

  • a parameter usually has one common value
  • you want to make part of the function optional

If you want to practice more function calls, see how to use default arguments in Python and return values in Python functions.

Common mistakes

Beginners often make these mistakes:

  • forgetting that default values make parameters optional, not required
  • placing a required parameter after a parameter with a default value
  • using a keyword name that does not match the function definition
  • mixing positional and keyword arguments in the wrong order
  • passing one argument by position and again by keyword

If you are unsure how a function is defined, these tools can help:

help(function_name)
print(function_name.__defaults__)
print(function_name.__code__.co_varnames)
print(function_name.__code__.co_argcount)

What they show:

  • help(function_name) shows documentation if available
  • function_name.__defaults__ shows default values
  • function_name.__code__.co_varnames shows parameter and local variable names
  • function_name.__code__.co_argcount shows how many positional parameters the function has

FAQ

What is the difference between default and keyword arguments?

A default argument gives a parameter a fallback value. A keyword argument is a way to pass a value using the parameter name.

Are keyword arguments required when a function has default values?

No. You can still call the function positionally if the order is correct.

Can I skip one positional argument and only pass a later one?

Not with positional arguments. Use a keyword argument to target a later parameter.

Example:

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

greet(name="Sam")

Why does Python say got multiple values for argument?

This usually means the same parameter was passed once by position and again by keyword.

Why must default parameters come after required ones?

Because Python matches positional arguments from left to right. Putting defaults first would make calls ambiguous.

See also