In this Python tutorial, we’ll learn about Python keyword arguments, their role and proper usage in Python programs. We’ll understand them using practical Python code examples.
Outline
- Introduction: Python Keyword Arguments
- Syntax of Keyword Arguments In Python
- Implementing Python Keyword Arguments (Easy Examples)
- Example 1: Using Positional Arguments
- Example 2: Using Python Keyword Arguments
- Example 3: Use Keyword Arguments After Positional Arguments
- Conclusion
Introduction: Python Keyword Arguments
We already know the concept of functions in Python. Click here if you want to learn about them in detail.
Arguments are used to pass data to a function and the function accept that data using its parameters. Passing the data directly comes in the form of positional arguments which means data of first argument will be assigned to the first parameter of function, second argument to the second parameter and so on.
But there are some cases where we want to get rid of the order boundaries or want to make the code more readable. That’s where Python keyword arguments comes into play. Using this method, we don’t need to care about the order of arguments. We can assign the value of first argument to the second parameter or any parameter we want. Same goes for other arguments as well.
Let’s now understand this concept using practical Python code examples.
Syntax of Keyword Arguments In Python
def fun(age, name, height) // body fun(name='Zeeshan, age=24, height=6)
- This is how we define keyword arguments in Python.
- We’ve to use the name of parameter and assign the value to it using assignment operator(=).
- Using this method, we don’t have to follow the order as shown in the above example.
Implementing Python Keyword Arguments (Easy Examples)
See below examples for proper understanding.
Example 1: Using Positional Arguments
def fun(name, age, height): print( f'Your name is {name}' ) print( f'Your age is {age}' ) print( f'Your height is {height}' ) fun('Zeeshan', 24, 6)
Output
Your name is Zeeshan Your age is 24 Your height is 6
This looks good as first parameter is specified for name and we’ve passed name as a first positional argument. Second parameter is specified for age and we’ve passed age as a second argument and so on.
Let’s now change the order and see what happens. See below:
fun(24, 6, 'Zeeshan')
Output
Your name is 24 Your age is 6 Your height is Zeeshan
We can see that positional arguments depends on the order. First argument is assigned to the first parameter, second one to the second parameter and so on.
This method is very hard when you are dealing with parameters in bulk. In order to solve it, we use Python keyword arguments.
Example 2: Using Python Keyword Arguments
We’ll be taking the same above example to demonstrate how keyword arguments can benefit us in that situation. See below:
fun(age=24,height=6,name='Zeeshan')
Output
Your name is Zeeshan Your age is 24 Your height is 6
We can see that even with this order, the right argument values has been assigned to the right parameters.
Example 3: Use Keyword Arguments After Positional Arguments
There might be a case where we want to use both keyword and positional arguments. In that situation, we should use keyword arguments after the positional arguments. If not, then a syntax error will occur. We’re using the same above example in this situation as well. See below code:
fun(age=24,6,'Zeeshan')
Output
SyntaxError: positional argument follows keyword argument
In order to solve it, we must always use positional arguments first, then we should use Python keyword arguments. See below code:
fun('Zeeshan', 24, height=6)
Output
Your name is Zeeshan Your age is 24 Your height is 6
As we can see that this works perfectly fine.
So this is how we can easily use Python keyword arguments. Its not necessary to use keyword arguments in every situation. Use them only when you need it.
Feel free to share this article with your squad of Python developers.
Also, don’t hesitate to ask if you still have any queries related to the implementation of Python keyword arguments. We’ll be super glad to solve all your queries.
Conclusion
As a conclusion of this tutorial, hope you now have an in-depth practical knowledge of how to properly use Python keyword arguments. We’d love to receive your informative feedback on this article.
Do visit our other posts on Python programming. Thank you for reading this one.