All you have to know about functions parameters in Python, just in 5 minutes.
There are some things you have to consider to get a better code when you define a function in python. And now I tell you how to use parameters and their kind they are.
Basically we have five types of parameters: non-default parameter, default parameter, optional parameter, arbitrary arguments (*args) and keywords arbitrary arguments (**kwargs).
All these parameters must be declared in the function in the following order:
def function(a, b, c = ‘ ‘, d = ‘ ‘, e = True, f = False, *args, **kwargs)
- a, b are non-default parameters.
- c, d are optional parameters.
- e, f are default parameters.
- *args are the arbitrary argument parameters.
- **kwargs are the keyword arbitrary argument parameters.
Now we are going to know what is each one.
Non-default parameters
These parameter are basic to any function. We can say that there is no function if there is no non-default parameters. To show how to use them, we can do a common example: calculation of a triangle area.
def triangle_area(base, height):
area = base*height/2
return area
In this function base and height are non-default parameters.
Now we can call our function to calculate a triangle area.