How to Use Default Parameters in Python Functions

Define a Python function with default parameters and call it using positional and keyword arguments.

Easy Python 3.9+ Aug 9, 2026 Functions & basics 13 views 0 copies

Python code

9 lines
Python 3.9+
def greet(name, greeting="Hello", punctuation="!"):
    """Concatenate a greeting message with default parameters."""
    return f"{greeting}, {name}{punctuation}"

if __name__ == "__main__":
    print(greet("Alice"))                 # Uses both defaults
    print(greet("Bob", "Hi"))             # Uses default punctuation
    print(greet("Sam", "Hey", "!!"))      # Overrides both defaults
    print(greet("Zoe", punctuation="?"))  # Uses keyword argument

Output

stdout
Hello, Alice!
Hi, Bob!
Hey, Sam!!
Hello, Zoe?

How it works

Default parameters allow a function to be called with fewer arguments than it defines. In this example, greet has two defaults (greeting and punctuation), so callers can omit them. When you call greet("Alice"), Python fills in the defaults, producing Hello, Alice!. Passing arguments by position (greet("Bob", "Hi")) or by keyword (greet("Zoe", punctuation="?")) overrides those defaults per call. This pattern makes functions flexible and reduces repetitive arguments.

Common mistakes

  • Defining non-default parameters after default parameters (raises SyntaxError).
  • Using mutable default values like lists or dicts, which share state across calls.
  • Forgetting that keyword arguments must follow positional arguments in a call.

Variations

  1. Use `def greet(name, greeting="Hello", punctuation="!") -> str:` to add a return type hint.
  2. Use a dataclass or a config object to pass many optional settings.

Real-world use cases

  • Building REST client functions where headers or timeouts have sensible defaults.
  • Creating logging helpers that let callers override severity or timestamp without breaking existing calls.
  • Writing CLI script helpers where optional flags default to safe values unless overridden.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Functions & basics

Related tutorials and quizzes for this topic.