The Python language has a different way of representing syntax and default values for function arguments. Default values indicate that if no argument value is given during the function call, the function argument will take that value. The default value is...
The Python language has a different way of representing syntax and default values for function arguments.
Default values indicate that if no argument value is given during the function call, the function argument will take that value. The default value is assigned by using the assignment (=) operator of the form keywordname=value.
# creating a function by giving default values def tutorialspoint(website, author ='XYZ', language ='Python'): print(website, "website article is written by the author", author,"of language", language)
On executing, the above program will generate the following output –
tutorialspoint website article is written by the author XYZ of the language Python tutorialspoint website article is written by the author Alex of the language J**a tutorialspoint website article is written by the author Gayle of the language Python tutorialspoint website article is written by the author C++ of language Python
Here in the first case, There is just one required argument in the first call, and the remaining arguments are set to default values.
In the second function call, we are calling a function with 3 positional arguments (website, author, language). The value of the author and standard parameters is changed from the default value to the new passing value.
On executing, the above program will generate the following output –
tutorialspoint website article is written by the author XYZ of language Python tutorialspoint website article is written by the author XYZ of language J**a tutorialspoint website article is written by the author Gayle of language Python
There is only one keyword parameter required in the first call.
In the second call, one parameter is necessary and one is optional (language), the value of which is changed from default to a new passing value.
We can see from the third call that the order of the keyword arguments is unimportant/not mandatory.
Now we some of the invalid cases of a function call that throw an error.
On executing, the above program will generate the following output –
Traceback (most recent call last): File "main.py", line 5, in <module> tutorialspoint() TypeError: tutorialspoint() missing 1 required positional argument: 'website'
No arguments are passed to the function, hence an error occurs
On executing, the above program will generate the following output –
File "main.py", line 6 tutorialspoint(website= 'tutorialspoint', 'Alex') ^ SyntaxError: positional argument follows keyword argument
After a keyword argument, there is a non-keyword argument for author (Alex) (tutorialspoint). As a result, an error occurs.
On executing, the above program will generate the following output –
Traceback (most recent call last): File "main.py", line 6, in <module> tutorialspoint(address ='Hyderabad') TypeError: tutorialspoint() got an unexpected keyword argument 'address'
Because the keyword address is not defined in the function (unknown keyword argument), an error is raised.
It must be done extremely carefully. The reason for this is the default values of the arguments are evaluated only once when the control reaches the function.
For the first time, a definition. Following that, the same values (or mutable objects) are referenced in subsequent function calls.
['hello'] ['hello', 'tutorialspoint'] ['hello', 'tutorialspoint', 'python']
We learned about default values in Python functions in this article. We learned about default arguments and parameters by using some examples.