Positional and Keyword Arguments

While programming in Python, one can use positional arguments, keyword arguments, or a blend of both when defining and calling functions. The use of these arguments can enhance code readability and flexibility.

Positional Arguments

Positional arguments are those which must be passed to a function in the proper positional order. Here's an example where we build a simple function to generate a full name:
def full_name(first_name, last_name):
    return f'{first_name} {last_name}'  # Combine the first and last name

print(full_name('Anna', 'Brown'))       # Prints 'Anna Brown'
In the above example, Anna is the first argument, and Brown is the second argument. If we reverse the order of the arguments, we'll see different results:
print(full_name('Brown', 'Anna'))       # Prints 'Brown Anna'

Keyword Arguments

In contrast, keyword arguments enable us to specify the argument's name when we pass it to the function, thus enhancing the readability of our code. Additionally, we can use them to establish default values for optional arguments.
def full_name(first_name, last_name):
    return f'{first_name} {last_name}'

print(full_name(last_name='Brown', first_name='Anna'))  # Prints 'Anna Brown'
print(full_name(first_name='Anna', last_name='Brown'))  # Prints 'Anna Brown'
This provides the same output as the first example, regardless of the argument order.
 
💡
It's important to note that keyword arguments must always follow positional arguments when calling a function.
If keyword arguments are used before positional arguments in a function call, Python will raise a syntax error. This rule helps Python differentiate between the two. If the positional arguments follow keyword arguments, Python's interpreter gets confused because it expects all positional arguments to have already been defined. Here's an example of an incorrect function call that would raise a SyntaxError:
def greet(first_name, last_name):
    print('Hello', first_name, last_name)

greet(last_name='Doe', 'John')
# SyntaxError: positional argument follows keyword argument
This rule ensures that the function calls are unambiguous, making the code easier to understand and less prone to bugs.

Combining Positional and Keyword Arguments

Consider this function that formats a full name, including a middle name:
# Default values for middle and last names (empty strings)
def full_name(first, middle='', last=''):
    return first + ' ' + middle + ' ' + last

print(full_name('Anna', last='Brown'))                   # 'Anna  Brown'
print(full_name('Anna', middle='Louise', last='Brown'))  # 'Anna Louise Brown'
print(full_name('Anna', 'Louise', last='Brown'))         # 'Anna Louise Brown'
print(full_name('Anna', 'Louise', 'Brown'))              # 'Anna Louise Brown'
print(full_name(first='Anna', 'Louise', last='Brown'))   # SyntaxError
In this example, the first name is provided as a positional argument, while the last name is a keyword argument. The middle name isn't provided, so it uses its default value (an empty string).
One more important rule is that once you've started using keyword arguments, all following arguments should be keyword arguments. For instance, print(full_name(first='Anna', 'Louise', last='Brown')) would generate an error because a positional argument (Louise) is following a keyword argument (first='Anna').
Finally, let's consider a more complex real-world example: a function that calculates the total cost of a purchase with optional parameters for tax and discount.
# Default values for tax and discount are set to 0
def calculate_total_cost(price, tax=0.0, discount=0.0):
    return price + price * tax - discount

print(calculate_total_cost(100, tax=0.05))                # 105.0
print(calculate_total_cost(100, discount=10))             # 90.0
print(calculate_total_cost(100, tax=0.05, discount=10))   # 95.0
In this case, the price is a required positional argument, while tax and discount are optional keyword arguments. If not provided, they will take on their default values of 0.
 
To check your solution you need to sign in
Sign in to continue