Have you ever wondered what asterisks are in a function or what KWARGS and ARGS are? Within the programming fundamentals, there are a series of functions that are responsible for processing input data, performing an action and returning values. In many of the problems solved in programming languages such as Python, there are functions with a variable number of arguments. This is where the programmer must know what is KWARGS and ARGS in Python.
Therefore, in this post, we want to tell you what KWARGS and ARGS are, what their functionality is and how you can use them in your programs.
What is KWARGS?
To explain what KWARGS and ARGS are, we are going to start by talking about KWARGS, which is one of the ways to create a function with a variable number of arguments. It is necessary when we want to perform a function that have the parameters we want, although are not of the same type or do not have the same meaning. This function commonly appears with two asterisks **. KWARGS is a local dictionary with an indeterminate number of values.
So that your functionality is much clearer, we show you the following example, which presents a function that lists the grades of a student from a high school course to get your mean:
def bulletin(**subjects):
notes = [ ]
for subject, grade in subjects:
print(subject, «-«, note)
notes.append(note)
print(«-» * 20)
print(«Average:», sum(notes)/len(notes)
bulletin(Language=10, Mathematics=8, English=4)
Example 1: KWARGS.
In the previous example, a bulletin function is defined, through which arguments will be passed by key-value pairs, that is, subjects. Here you can see how Python uses the kwrgs function to form a dictionary titled subjectsthat is to say, the local variable of the bulletin. The dictionary is made up of the pairs language=10, mathematics=8 and English=4. Python then processes the dictionary into string variables.
What is ARGS?
To continue with the explanation of what KWARGS and ARGS are, we continue with args.
To create a function with a variable or indeterminate number of parametersusually of the same type, we use the function *ARGS, which commonly appears only with an asterisk, unlike the KWAGS function. The need arises, for example, when calculating a mean or when processing a function with a variable number of arguments. ARGS is a local list with an undetermined number of values.
To make its functionality much clearer, we will take the following example, which represents a function that returns the sum of several numbers:
def sum(*nums):
total = 0
for nums in nums:
total += num
return total
print(sum(4, 5, 6))
Example 2: ARGS
In the previous example, a function is defined with a variable number of parameters, with an asterisk in front. You can see how Python, through the ARGS function, takes the values 4, 5, 6 and copies them to a list called numberswhat is the local variable in sum of the function. In the second step, that is processed list as what it is, with the same values mentioned (4, 5, 6) and without putting the asterisk in the first place.
And so, we saw what KWARGS and ARGS are.
What is the next step to implement these features?
Now that you know what kwargs and args are in the Python programming language, you can start using these functions to solve your problems in the field of programming. To learn more in depth how to use these and other essential functions for your path as a programmer, do not hesitate to sign up for the Introduction to Programming Course from Scratch that offers.