Module 3 Lesson 4: Default and Keyword Arguments
·Programming

Module 3 Lesson 4: Default and Keyword Arguments

Make your functions more flexible. Learn how to provide default values and how to pass arguments using their names instead of just their position.

Module 3 Lesson 4: Default and Keyword Arguments

In the previous lesson, we learned that arguments must be passed in the correct order. But as your functions get more complex, memorizing the order of 5 or 10 parameters becomes impossible. In this lesson, we’ll learn how to set Default Values and use Keyword Arguments to make our code more readable and flexible.

Lesson Overview

In this lesson, we will cover:

  • Default Parameters: Making arguments optional.
  • Keyword Arguments: Assigning values by name.
  • Mixing Positional and Keyword: The rules of engagement.
  • Why this matters: Writing "cleaner" and more robust code.

1. Default Parameters (Optional Inputs)

You can give a parameter a default value using the = sign in the definition. If the user doesn't provide an argument, Python uses the default.

def greet(name, message="Welcome to the team!"):
    print(f"Hello {name}, {message}")

# 1. Use the default message
greet("Alex") # Output: Hello Alex, Welcome to the team!

# 2. Provide a custom message
greet("Sara", "Good morning!") # Output: Hello Sara, Good morning!

Rule: Parameters with default values must always come AFTER parameters without default values.


2. Keyword Arguments (Named Inputs)

When calling a function, you can specify which parameter you are providing by using its name. This means the order no longer matters!

def create_profile(name, age, city):
    print(f"{name} is {age} years old and lives in {city}.")

# Normal positional call (must follow the order: name, age, city)
create_profile("Alex", 25, "New York")

# Keyword call (Can be any order!)
create_profile(city="London", name="Sara", age=30)

Benefit: This makes it much clearer what each number or string represents when someone else reads your code.


3. Mixing Positional and Keyword

You can mix both, but there is one strict rule: All positional arguments must come BEFORE keyword arguments.

# VALID
create_profile("Bill", city="Paris", age=40)

# INVALID (This will cause an error!)
# create_profile(name="Bill", 40, "Paris")

Practice Exercise: The Custom Pizza

Create a file named pizza_order.py.

  1. Define a function order_pizza(size, crust="Thin", topping="Cheese").
  2. The function should print: "Checking out: [Size] pizza with [Crust] crust and [Topping] topping."
  3. Call the function three times:
    • Once with only the size "Large" (uses defaults).
    • Once with size "Medium" and topping "Pepperoni" (uses keyword for topping).
    • Once using keywords for all three in a random order.

Quick Knowledge Check

  1. How do you set a default value for a parameter?
  2. If you have a function print_info(name, age=20), can you call it as print_info(age=30, "Alex")? Why or why not?
  3. What are the two main benefits of using Keyword Arguments?
  4. Where must default parameters be placed in a function definition?

Key Takeaways

  • Default values make arguments optional.
  • Keyword arguments allow you to pass data by its labels.
  • Keyword arguments make code "self-documenting" (it explains itself).
  • Always put positional arguments before keyword arguments.

What’s Next?

We know how to pass data into a function. But where does that data "live"? In Lesson 5, we’ll explore Variable Scope to understand the difference between variables inside and outside a function!

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn