Keyword Arguments

Module: M3-R5: Python Programming

Chapter: Functions

🔹 What are Keyword Arguments?

Keyword arguments allow you to pass values to a function using the parameter names. This makes your function calls more readable and allows arguments to be passed in any order.

Syntax:

def function_name(param1, param2):
    # code block

function_name(param1=value1, param2=value2)

Example:

def greet(name, age):
    print(f"Hello {name}, you are {age} years old")

# Using keyword arguments
greet(age=24, name="Sourav")

Output:

Hello Sourav, you are 24 years old
🔹 Combining Positional and Keyword Arguments

Positional arguments must come first, followed by keyword arguments.

def order(item, quantity=1):
    print(f"Order: {quantity} {item}")

# Valid calls
order("Pizza")                 # Uses default quantity
order("Burger", quantity=2)    # Overrides default

Output:

Order: 1 Pizza
Order: 2 Burger
🔹 Advantages of Keyword Arguments
  • Improves readability of function calls.
  • Allows passing arguments in any order.
  • Reduces errors when many arguments are present.
✅ Summary
  • Keyword arguments are passed by explicitly naming parameters in the call.
  • They can be combined with default arguments.
  • Always place positional arguments before keyword arguments.
Quick Links