Skip to main content
  • Python is available now. All other compilers are coming soon.

    Python
  • Popular Tutorials

    • Getting Started With Python
    • Python if Statement
    • while Loop in Python
    • Python Lists
    • Dictionaries in Python
    All Python Tutorials

    Reference Materials

    Built-in FunctionsList MethodsDictionary MethodsString Methods
    View all

Online Compilers

  • Python Compiler
  • Python Playground
  • Python Projects
  • Exercises

Tutorials

  • Learn Python
  • Python for Beginners
  • Variables and Types
  • Python Strings
  • Python Lists
  • Python If Else
  • Python For Loops
  • Python Functions
  • Python Classes

Examples

  • Python Examples
  • Hello World
  • Add Two Numbers
  • Even or Odd
  • Factorial
  • Fibonacci
  • Prime Check
  • Palindrome Check

Company

  • About
  • Contact
  • Privacy Policy
  • Terms of Use
  • Disclaimer

© 2026 ILoveCompiler. All rights reserved.

  1. Home
  2. Python Tutorial
  3. Python Function Arguments: Defaults, Keyword, and *args

Functions · Tutorial 20

Python Function Arguments: Defaults, Keyword, and *args

Use positional and keyword arguments, default values, *args, **kwargs, and avoid mutable default pitfalls.

Open the compilerPractice exercises
intermediate9 min readUpdated November 19, 20264 learning goals
On this page
  1. Positional arguments
  2. Keyword arguments
  3. Default values
  4. Keyword-only arguments
  5. args
  6. kwargs
  7. The mutable default trap
  8. Unpacking arguments at the call site
  9. Practice

What you will learn

  • Pass arguments by position and by keyword
  • Define default parameter values safely
  • Accept variable numbers of arguments
  • Explain why mutable defaults are dangerous
Before this lesson: Python Functions: Define, Call, and Return Values

Function definitions can specify parameters in several ways. Choosing the right form makes calls readable and prevents bugs.

Positional arguments#

Python
def describe(name, age, city):
    return f"{name} is {age} and lives in {city}"


print(describe("Ada", 36, "London"))

Arguments are matched left to right. A wrong order produces wrong results without necessarily raising an error.

Keyword arguments#

Python
print(describe(age=36, city="London", name="Ada"))

Keyword arguments are matched by name, so order does not matter. They are especially useful for calls with several boolean or numeric parameters where positions are hard to remember.

A call may mix positional and keyword arguments, but positional arguments must come first:

Python
print(describe("Ada", city="London", age=36))

Default values#

Python
def power(base, exponent=2):
    return base ** exponent


print(power(5))       # 25
print(power(5, 3))    # 125
print(power(2, exponent=10))  # 1024

Parameters with defaults must come after parameters without defaults. Defaults are evaluated once, when the function is defined.

Keyword-only arguments#

Put * in the parameter list to require keyword arguments for everything after it:

Python
def connect(host, *, timeout=30, retries=3):
    return f"{host}: timeout={timeout}, retries={retries}"


print(connect("example.com", timeout=10))
# connect("example.com", 10)  # TypeError

This prevents confusing positional calls when several parameters share a type.

*args#

*args collects extra positional arguments into a tuple:

Python
def add_all(*numbers):
    return sum(numbers)


print(add_all(1, 2, 3, 4))

The name args is a convention; what matters is the *. Inside the function, numbers is a tuple.

**kwargs#

**kwargs collects extra keyword arguments into a dictionary:

Python
def show_settings(**options):
    for key, value in options.items():
        print(f"{key} = {value}")


show_settings(theme="dark", font_size=14)

The name kwargs is also a convention. These tools are useful for wrappers and configuration, but a function that accepts arbitrary options can be hard to document. Prefer explicit parameters when the set is known.

The mutable default trap#

Do not use a mutable object as a default:

Python
def add_item(item, items=[]):   # BUG: list shared across calls
    items.append(item)
    return items

The default list is created once. Every call that omits items reuses the same list. Use None as the sentinel:

Python
def add_item(item, items=None):
    if items is None:
        items = []
    items.append(item)
    return items

This creates a fresh list for each call when no list is supplied.

Defaults are created once

def f(x=[]) and def f(x={}) share the same mutable default across every call. Use None and create the object inside the function.

Unpacking arguments at the call site#

Use * and ** to pass an existing sequence or mapping:

Python
def area(width, height):
    return width * height


dimensions = (3, 4)
print(area(*dimensions))

options = {"width": 5, "height": 6}
print(area(**options))

This is common when forwarding arguments to another function.

Practice#

Write a function format_price(amount, currency="USD", *, decimals=2) that returns a formatted string. Call it with defaults, with a positional currency, and with keyword arguments. Then write a function that accepts any number of scores with *scores and returns the highest and average.

Previous tutorialPython Functions: Define, Call, and Return ValuesNext tutorialPython Variable Scope and Object Lifetime

Related tutorials

intermediate

Python Variable Scope and Object Lifetime

Understand local, global, and nonlocal scope, when variables are created, and why modifying globals inside functions is risky.

8 min read
intermediate

Python Error Handling with try, except, else, and finally

Read tracebacks, catch specific exceptions, raise your own errors, and clean up resources reliably.

9 min read

On this page

  1. Positional arguments
  2. Keyword arguments
  3. Default values
  4. Keyword-only arguments
  5. args
  6. kwargs
  7. The mutable default trap
  8. Unpacking arguments at the call site
  9. Practice

Run the examples

Every Python block has a Run in compiler button that opens the code in a new tab.