Python Functions
Estimated reading: 3 minutes 261 views

Python Default Arguments – Complete Guide with Examples

Introduction – Why Use Default Arguments?

In Python, functions are powerful because they offer flexibility—and default arguments make them even more adaptable.

With default arguments, you can assign a default value to parameters. This lets users call your function with fewer arguments while still giving them the option to override those defaults.

Default arguments are essential when designing APIs, utilities, or any code that benefits from optional parameters.

In this guide, you’ll learn:

  • How default arguments work in Python
  • The order and rules of default parameters
  • Real-world use cases
  • Best practices and common pitfalls

Syntax of Default Arguments

def function_name(param1=value1, param2=value2):
    # function body
  • Parameters with default values must come after non-default parameters.

Example 1: Function with One Default Parameter

def greet(name="Guest"):
    print(f"Hello, {name}!")
greet()           # Output: Hello, Guest!
greet("Alice")    # Output: Hello, Alice!

Explanation:
If no argument is passed, "Guest" is used by default.


Example 2: Multiple Default Parameters

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

print(power(3))       # 9
print(power(3, 3))    # 27

Use Case:
Let users compute square by default, or choose their own exponent.


Example 3: Incorrect Parameter Order

#  This will raise a SyntaxError
# def example(a=1, b): 
#     pass

Warning:
Default arguments must come after all required (non-default) ones.


Real-World Example: Sending Emails with Optional Subject

def send_email(to, subject="No Subject", body=""):
    print(f"To: {to}")
    print(f"Subject: {subject}")
    print(f"Body: {body}")

send_email("user@example.com")

Output:

To: user@example.com
Subject: No Subject
Body:

Tip: Default arguments are ideal for optional parameters like subject, headers, or timeout.


Combining Positional and Default Arguments

def book_flight(source, destination="New York"):
    print(f"Booking flight from {source} to {destination}")

book_flight("London")               # destination defaults to New York
book_flight("London", "Chicago")   # override default

Summary – Key Takeaways

  • Use default arguments to make parameters optional
  • Default values are assigned only if no argument is passed
  • Always put default parameters after non-default ones
  • Great for setting fallback values and simplifying function calls

FAQ Section

Can default arguments be functions or objects?

Yes, but mutable defaults (like lists or dicts) should be avoided directly. Use None and set defaults inside the function.

Can I override default arguments?

Yes! Just pass a value in the function call.

Are default arguments stored in memory?

Yes, the default values are evaluated once at function definition time.

Can I mix default and non-default arguments?

Yes, but all non-default arguments must come first.

How are default arguments different from *args and **kwargs?

  • default arguments give fallback values.
  • *args accepts variable non-keyword arguments.
  • **kwargs accepts variable keyword arguments.

Share Now :
Share

Python Default Arguments

Or Copy Link

CONTENTS
Scroll to Top