Mastering Python Function Arguments: Positional, Keyword, and Default Parameters
Mastering Python Function Arguments
Discover how to design functions that accept variable numbers of arguments in Python—using defaults, keywords, and arbitrary parameters for cleaner, more adaptable code.
Video: Python Function Arguments: Positional, Keywords and Default
Arguments
In the user‑defined function topic, we covered defining and invoking functions. A call that mismatches the expected signature raises an error. For example:
def greet(name, msg):
"""This function greets the person with the provided message."""
print("Hello", name + ", " + msg)
greet("Monica", "Good morning!")
Output
Hello Monica, Good morning!
The greet() function accepts two parameters. Calling it with the correct number of arguments runs smoothly; otherwise, the interpreter emits a TypeError with a clear message.
>>> greet("Monica") # only one argument
TypeError: greet() missing 1 required positional argument: 'msg'
>>> greet() # no arguments TypeError: greet() missing 2 required positional arguments: 'name' and 'msg'
Variable Function Arguments
Python offers flexible ways to accept a variable number of arguments. Three common patterns are covered below.
Python Default Arguments
Parameters can be given default values using the assignment operator (=). The following example demonstrates how an optional message defaults to “Good morning!”:
def greet(name, msg="Good morning!"):
"""
This function greets the person with the
provided message. If the message is not
supplied, it defaults to "Good morning!".
"""
print("Hello", name + ", " + msg)
greet("Kate")
greet("Bruce", "How do you do?")
Output
Hello Kate, Good morning! Hello Bruce, How do you do?
In this definition, name is mandatory, while msg is optional. When a default argument is present, all subsequent parameters must also have defaults; otherwise a SyntaxError occurs.
def greet(msg = "Good morning!", name):
pass
SyntaxError: non-default argument follows default argument
Python Keyword Arguments
Python allows callers to specify arguments by name, enabling positional order flexibility. The same greet() function can be invoked as follows:
# 2 keyword arguments
greet(name = "Bruce", msg = "How do you do?")
# 2 keyword arguments (out of order)
greet(msg = "How do you do?", name = "Bruce")
# 1 positional, 1 keyword argument
greet("Bruce", msg = "How do you do?")
Mixing positional and keyword arguments is permissible only when all positional arguments precede keyword ones; otherwise a SyntaxError results.
greet(name="Bruce","How do you do?")
SyntaxError: non-keyword arg after keyword arg
Python Arbitrary Arguments
When the number of arguments is unknown at design time, use an asterisk (*) to capture them as a tuple:
def greet(*names):
"""Greets each name in the supplied tuple."""
for name in names:
print("Hello", name)
greet("Monica", "Luke", "Steve", "John")
Output
Hello Monica Hello Luke Hello Steve Hello John
Inside the function, names is a tuple containing all passed arguments, which can then be iterated or processed as needed.
Python
- Master Python Functions: Syntax, Types, and Practical Examples
- Mastering Python Recursion: How Functions Call Themselves
- Python Lambda Functions: A Practical Guide to Anonymous Functions
- Python Generators: Efficient Iteration and Advanced Use Cases
- Python Closures Explained: How Nested Functions Capture Variables
- Mastering Python Decorators: Enhance Functions with Expert Techniques
- Mastering Python Functions: Definition, Calling, Indentation, Arguments & Return Values
- Python round() Function Explained with Practical Examples
- Mastering Python's map() Function: Syntax, Examples, and Best Practices
- Python Functions Explained: Building Reusable Code Modules