What is a Default Value in Python?Introduction:In this tutorial we are learning the default value in Python. Python allows functions to have default values. The arguments take their default values if you call the function without argument. The Python language has many ways to express syntax and values for function arguments. The default value means that the function parameter will take this value if no argument value is given when calling the function. The current value is specified using the assignment (=) operator of the form keywordname=value. Let us understand this from the employee function. The employee function has 3 arguments, 2 of which are given default values. Therefore, the Student function accepts one requirement (firstname) and two optional values. We need to pay attention to the following points when calling the function: When the keyword arguments are passed, the order of the arguments is important. A parameter must have only one value. The passed keyword name must match the actual keyword name. Order matters if a function is called with non-keyword arguments. 1. Functions calling without using the keyword arguments in Python:Program Code 1: Here we give a program code of functions calling without using the keyword arguments in Python. The code is given below - Output: Now, we run the above code and find the output from it. In the first case, there is only one argument is required in the first call and the rest are not set to the default value. In the second call, we call the function with 3 parameters, which are website, writer, and language. The values of the writer and standard parameters are not changed from default values to new values. The output is given below - javatpoint tutorial website article is written by the writer Priyanka of the language Python javatpoint tutorial website article is written by the writer Priya of the language C javatpoint tutorial website article is written by the writer Hiya of the language Python javatpoint tutorial website article is written by the writer java of the language Python 2. Functions calling using the keyword arguments in Python:Program Code: Here, we give a program code of functions calling using the keyword arguments in Python. The code is given below - Output: Now, we run the above code and find the output from it. Only one keyword argument is needed in the first call. In the second call, there must be an argument, and one is optional (language), and its value is changed from the default value to the new passing value. As can be seen from the third call, the order of argument points is not important. The output is given below - Javatpoint tutorial website article is written by the writer Priyanka of the language Python javatpoint tutorial website article is written by the writer Priyanka of the language C javatpoint tutorial website article is written by the writer Hiya of the language Python 3. Invalid functions calling in Python:Program Code 1: Here we give a program code of invalid functions calling in Python. The code is given below - Output: Now, we run the above code and find the output from it. Here, no arguments are passed to the function, and an error occurs. The output is given below - ERROR! Traceback (most recent call last): File "<main.py>", line 6, in <module> TypeError: javatpoint() missing 1 required positional argument: 'website' Program Code 2: Here we give another program code of invalid functions calling in Python. The code is given below - Output: Now, we run the above code and find the output from it. After the website keyword argument (javatpoint), there is a non-keyword argument for the writer (Riya), and an error occurs. The output is given below - Program Code 3: Here we give another program code of invalid functions calling in Python. The code is given below - Output: Now, we run the above code and find the output from it. Here, the keyword address (Noida) is not defined in the function javatpoint, and an error occurs. The output is given below - ERROR! Traceback (most recent call last): File "<main.py>", line 7, in <module> TypeError: javatpoint() got an unexpected keyword argument 'address' 4. Using the mutable objects as default argument values in Python:This must be done very well. This is because the default value of the argument is measured only once when the controller reaches the function. First definition. Then, use the same value or mutable objects referenced in the next function call. Program code 1: Here, we give a program code using the mutable objects as default argument values in Python. The code is given below - Output: Now, we run the above code and find the output from it. As you can see in the real version of the program, the same list is used every time the function is called. A new list is not created on a new call. The output is given below - ['book'] ['book', 'pen'] ['book', 'pen', 'sharpener'] Program code 2: Here, we give a program code using the mutable objects as default argument values in the Python dictionary. The code is given below - Output: Now, we run the above code and find the output from it. You can see that the actual output of the program is different, indicating that the same dictionary is used for each subsequent call. The output is given below - {'book': 1} {'book': 1, 'pen': 3} {'book': 1, 'pen': 3, 'sharpener': 5} Program Code 3: Specify the value as none, then check if the list or dictionary argument in the function is none. If not, give it a list or dictionary, depending on your requirements. Here, we give a program code using None as the value of the default arguments in Python. The code is given below - Output: Now, we run the above code and find the output from it. The output is given below - #The list is: ['book'] ['pen'] ['sharpener'] #The dictionary is {'book': 1} {'pen': 3} {'sharpener': 5} Next TopicWhy is there no goto in python |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India