Arguments in Python

Tram Ho

Hello everyone In this article, I will introduce you to the concept of Arguments in Python. Let’s find out in our article!


Argument Concepts

In Python, you can define a function that takes a variable number of arguments. We’ve seen how to define a function and call it. Otherwise, the function call will result in an error. Here is an example.

Result :

In the example above the function greet() there are 2 parameters.

Because we called this function and passed 2 parameters, the function ran correctly and did not get an error.

If we call this function and pass in a missing parameter or pass no parameters. Then an error will occur. When the function is defined with many parameters, when calling the function, we need to pass all those parameters.

Here is an example error message when we call a function that passes a missing parameter:

Error when calling function greet which passed 1 parameter missing:

Error when calling function greet without passing parameters:


Variables of Function Arguments in Python

In Python, there are different ways to define a function that can take a variable number of arguments.

Here are three different ways to define it:

Default arguments in Python

Function arguments will have default values in Python.

You can provide a default value for an argument using the assignment operator toán(=). Here is an example:

Result :

In the above example, the parameter name will not have a default value and when calling the function, it is necessary to pass the name parameter to the function.

In contrast to the parameter msg has always been assigned the default value of "Good morning!". So if when calling the function we can pass parameters tham msg. If we pass a parameter, then msg will have value which is the value we pass in. If we do not pass the parameter, the value of msg default will always be "Good morning!". That is, with this function msg will be optional.


Keyword Arguments in Python

When we call a function with some values, these values are assigned to the arguments according to their position.

For example when we call the function greet() with calling parameters greet("Bruce", "How do you do?") . then value Bruce will correspond to the argument name and  "How do you do?" will be of the argument msg.

Python allows functions to be called using keyword arguments. When we call functions in this way, the order (position) of the arguments can be changed. The following calls to the function greet() the above are all valid and produce the same result.

As we see, we can change positional arguments with keyword arguments during function call. But we must note that keyword arguments are subject to positional arguments.

Having a positional argument after the keyword arguments will result in an error. For example:

Result returned error:


Optional arguments in Python

There are cases where we may not know in advance how many arguments will be passed to a function. In this case, Python will give us a solution that is to call the function with an optional number of arguments.

While defining the function, we will use the asterisk (*) before the parameter name to denote this type of argument. Here is an example.

Result :

In the above example we have called the function with multiple arguments. These arguments are wrapped into a tuple before being passed to the function. Inside the function, we use a for loop to get back all the arguments.


Conclude

Below I have introduced you to the concept of Arguments in Python. If you have any questions, please leave a comment below.


Refer to more details

https://www.programiz.com/python-programming/object-oriented-programming

Share the news now