What is a default value in Python?

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.

Example

# 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)

Function call without keyword arguments

Example

 
# 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 the language", language) # calling only 1 positional argument(website) # here it takes the given default values for author(XYZ) and language(Python) tutorialspoint('tutorialspoint') # calling 3 positional arguments (website, author, language) tutorialspoint('tutorialspoint', 'Alex', 'J**a') # calling 2 positional arguments (website, author) # here it takes the given default value for the language(Python) tutorialspoint('tutorialspoint', 'Gayle') # here it takes the given default value for author(XYZ) tutorialspoint('tutorialspoint', 'C++')

Output

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

Explanation

  • 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.

Function call with keyword arguments

Example

 
# 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) # calling only 1 positional argument(website) with keywords # here it takes the given default values for author(XYZ) and language(Python) tutorialspoint(website= 'tutorialspoint') # calling 2 positional arguments (website, language) with keywords tutorialspoint(website= 'tutorialspoint', language = 'J**a') # calling 2 positional arguments (author, website) with keywords # the order of the keywords is not mandatory # here it takes the given default value for language(Python) tutorialspoint(author= 'Gayle', website= 'tutorialspoint')

Output

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

Explanation

  • 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.

Invalid Function Calls (Raises an Error)

Now we some of the invalid cases of a function call that throw an error.

Example

 
# 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) # No arguments are passed to the function, hence an error occurs tutorialspoint()

Output

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

Example

 
# 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) # There is a non-keyword argument for author(Alex) after # a keyword argument website(tutorialspoint). Hence an error occurs tutorialspoint(website= 'tutorialspoint', 'Alex')

Output

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.

Example

 
# 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) # The keyword address is not defined in the function(unknown keyword argument) # hence it raises an error tutorialspoint(address ='Hyderabad')

Output

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.

Using mutable objects as default arguments

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.

Example

 
# Creating a function by passing the list element, new list as arguments # the function returns a new list after appending elements to it def appendingElement(listElement, newList = []): newList.append(listElement) # returning a new list return newList # calling function by passing the list element as an argument # and printing the result new list print(appendingElement('hello')) print(appendingElement('tutorialspoint')) print(appendingElement('python'))

Output

['hello']
['hello', 'tutorialspoint']
['hello', 'tutorialspoint', 'python']

Conclusion

We learned about default values in Python functions in this article. We learned about default arguments and parameters by using some examples.

评论0

首页 导航 会员 客服