Python tutorial to call a function using keyword argument
Python tutorial to call a function using keyword arguments :
Most new python developer finds ‘keyword arguments’ confusing. If you are coming from a different programming language like Java, this might be a new thing for you.
In this python tutorial, we will learn what is keyword arguments and how to use Keyword arguments in a function. I will try to explain it as simply as possible. If you find it hard to understand, don’t hesitate to drop one comment below.
Normally, a function in python looks like :
Here, first and second are both parameters of the function. To call this function we need to pass values for both of the parameters first and second. If we don’t pass any value to any of these parameters, it will not run. These are known as default arguments.
Keyword arguments are optional arguments. By default, they have one value assigned in the definition of the function. If you don’t set any value while calling the function, it will take the default value. But, if you set any value, it will take the new value. I will explain it with an example below.
Keyword Arguments :
To understand Keyword Argument, let’s take a look at the below program :
- This program takes three arguments. First one, name is a default argument. We will always have to add it.
- Argument message is a keyword argument. If we don’t pass any value to this argument, it will print out the value mentioned in the definition.
- Argument secondMessage is also a keyword argument as like above. It will take the assigned value if we don’t pass anything for it.
Pass value for default keyword :
If we call the above method with only ‘name’ argument with value “Alex”, it will produce the below output :
So, it will take the value of name and takes the default values for the other keyword arguments.
The above line will also print the same output. We are only passing the ‘name’ argument with value ‘Albert’.
Passing value to a keyword argument :
Hello Albert !! Welcome Again!! Subscribe to get learn more tutorials like this.
In the above example, we are passing values for ‘name’ and ‘message’. As you can see, the message is changed in the output.
The below method call will print the same output :
welcomeUser(message = "Welcome Again!!",name = "Albert")
But, if we don’t mention the name, it will throw an error. Because the compiler doesn’t know what argument to use for that unnamed value.
welcomeUser(message = "Welcome Again!!", "Albert”)
It will throw one error. “Albert’ is for “name” or for “secondMessage”, we haven’t mentioned here.
welcomeUser(message = "Welcome Again!!"")
It will also throw the same error because the value for the default keyword name should be mentioned.
Python argument handling is really beautiful than any other programming language. Using argument name with its value makes the code more readable. If you are calling a function from a different file, try to use keyword arguments if the argument count is huge for any function.
Try to run the examples above and drop one comment below if you have any queries.