Back to course

Default Arguments and Optional Parameters

Python Programming: The 0 to Hero Bootcamp

Default Arguments

Default arguments allow you to make some function parameters optional. If the caller does not provide a value for that parameter, the function uses the default value defined in the function signature.

Defining Defaults

Default values are set using the assignment operator (=) in the parameter list.

python def send_email(recipient, subject='No Subject', body='Empty email.', sender='admin@app.com'): print("--- Sending Email ---") print(f"To: {recipient}") print(f"Subject: {subject}") print(f"From: {sender}") print(f"Body: {body}\n")

Calling with Defaults

  1. Providing all arguments (overriding defaults):

    python send_email('user@a.com', 'Urgent', 'Check logs now!')

  2. Using default values (omitting optional args):

    python send_email('report@b.com')

    Uses default subject, body, and sender

  3. Mixing positional and keyword arguments:

    python

    Specify only the subject, using the default body and sender

    send_email('sales@c.com', subject='Q3 Results')

Rule: Parameters without default values (required parameters) must always be defined before parameters with default values.