Keywords and Positional Arguments in Python

In Python, function arguments play a crucial role in defining and customizing the behaviour of functions. we have two types of passing values to the function parameters:

  1. Keyword arguments
  2. Positional arguments

In this article, we are going to explore the above two ways we can pass values to the function parameters.

so, let us consider the first way.

Keyword Arguments

We rarely use this method in our daily practices. Keyword arguments are a way to pass arguments to a function by explicitly specifying the parameter names along with their corresponding values. This approach enables you to pass the arguments in any order, regardless of how they are defined in the function.

Example 1

Here is an example program to pass the values to function parameters by specifying the parameter names:

Program

Output:

Hello Bob, you are 25 years old!

Explanation

Here, in the above example program, we used keyword arguments irrespective of the order in the function definition.

"Bob" is assigned to the name parameter, and 25 is assigned to the age parameter, even though the order differs.

Example 2

Here is one more example of describing the behaviour of keyword arguments:

Program

Output:

Hi, Alice!
Hello, Bob?
Hello, Charlie!

Explanation

In the provided code snippet, a greet function takes three parameters: name, greeting, and punctuation. The greeting and punctuation parameters have default values assigned to them, making them optional when calling the function.

While calling the function for the first time, we used keyword arguments for "greeting" and "name" Calling for the second time. Also, we used keyword arguments for punctuation and the Calling function for the third time. We only used one keyword argument, which is the name.

These various ways of calling the function demonstrate the flexibility of using keyword arguments with default values. You can specify values for specific parameters by using their names when calling the function, and the remaining parameters will take their default values if not explicitly provided.

Positional Arguments

This method is commonly used in our daily programming. Positional arguments are the most common type of argument. They are passed to a function in the order in which the parameters are defined. The position of the argument determines which parameter it will be assigned to.

Example 1

Here is a simple example program to demonstrate the positional arguments:

Program

Output:

Name: Alice
Aadhar number: 819203020768
Your name is Alice, and Aadhar number 819203020768 has been updated in the database.

Explanation

Here, we have directly passed the values to the function parameters without explicitly mentioning the parameter names. The function takes parameters in the provided order and prints the name and Aadhar number.

Example 2

Let us consider an example program to see how it can provide issues with positional argument:

Program

Output:

The result of 5 raised to the power of 6 is: 15625
The result of 6 raised to the power of 5 is: 7776

Explanation

This example program has a function named calculate_power, which takes two values: one is base, and the other one is exponent.

When we called the function with the same values but changed the positions, we changed the results. That means changing the order of the arguments will change the results and may cause issues in the future.

A mix of keyword and positional arguments

The combination of keyword and positional arguments provides greater flexibility to programmers. Using positional arguments to define some parameters and explicitly specifying parameter names for others can improve code maintainability and readability. This method is particularly useful when dealing with functions with many parameters, some of which have default values. Being able to override default values selectively using keyword arguments while relying on the order for others makes code more expressive and adaptable to different requirements.

Example 1

Here is an example program that demonstrated the combination of keyword and positional arguments in Python:

Program

Output:

Hello, Alice! You are 25 years old and live in Unknown.
Hello, Bob! You are 30 years old and live in New York.
Hi, Charlie! You are 22 years old and live in Unknown.
Hello, David! You are 28 years old and live in London.

Explanation

The following code example demonstrates how default values work in Python functions. When a function is defined, parameters can have default values assigned to them. The argument value is used if the function is called with an argument for that parameter. The default value is used if the function is called without an argument for that parameter.

For instance, the "print_info" function takes four parameters: "name", "age", "city", and "greeting". The default value is used if a value is not passed for any of them. In the first call, "Alice" is assigned to the "name" parameter as a positional argument, and 25 is assigned to the "age" parameter. The "city" and "greeting" parameters take their default values, which are "Unknown" and "Hello", respectively. Similarly, in the second call, "Bob" is assigned to the "name" parameter, 30 to the "age" parameter, and "New York" to the "city" parameter as a positional argument. The "greeting" parameter takes its default value.

Using default values, you can make your code more concise and readable.

Example 2

Let us see in which order we can send the arguments to the function:

Program

Output:

ERROR!
File "<string>", line 8
    example_function(positional_arg1=1, 2, keyword_arg1= "custom_value" )
                                                                      ^
SyntaxError: positional argument follows keyword argument

Explanation

The error message "SyntaxError: positional argument follows keyword argument" means you have mixed up the order of positional and keyword arguments while defining a function. It's important to note that in Python, positional arguments should always come before keyword arguments in the function definition.

Note: Positional arguments must be placed before keyword arguments in the function definition.

Difference Between Keyword and Positional Arguments

Here is a tabular representation highlighting the differences between keyword and positional arguments in Python:

FeaturePositional ArgumentsKeyword Arguments
SyntaxPassed based on the order of parametersPassed by explicitly specifying parameter names.
Order MattersThe order of arguments must match the parameter order.The order of arguments can be different from the parameters.
Default ValuesBy default, no values are automatically assigned as default values.You can assign default values to parameters.
FlexibilityLess flexible when changing argument order.More flexible and order-independent if names are used.
ReadabilityRelies on understanding parameter order.Improved readability and self-documenting.
Raises an errorAll parameters must be provided, or errors occur.If certain parameters are not specified, the default values will be used instead, provided they have been defined.
Examplegreet("Alice" ,"Hi")greet(name="Alice", greeting="Hi")

Conclusion

Python's keyword and positional arguments are important for function call flexibility and readability. Positional arguments follow parameter order, while keyword arguments assign values by name, offering flexibility and clarity. Keyword arguments also support default values for optional parameters. The choice between these argument types depends on the specific use case, with developers leveraging positional arguments for simplicity and keyword arguments for enhanced clarity and flexibility in more complex scenarios.