Argument in Python


The information in python can be passed into functions in the form of Arguments. Arguments are always specified after the name of the function. These are always written in parenthesis and you can separate each argument with a comma.

Arguments in python are specified after the function name, inside the parenthesis. You can add several arguments you wish to but you need to separate them with a comma. Here the information is passed into functions in the form of arguments. Arguments are also known by the name of pass by reference.

Let's take an example of Arguments:-

So as you can see we had assigned a value (Sharma) and it will be passed to all the arguments which come under fname. So we can get as many outputs as many arguments are passed in the function.

Types of Arguments

There are 4 types of Arguments in Python Programming Language:-

  1. Required arguments
  2. Keyword arguments
  3. Default arguments
  4. Variable-length arguments

Required Argument

The arguments which are passed to a function in correct positional order are known as a required argument. But in this case, we get a syntax error like this as shown in the example given below:-

So you can see we got an error in the above code.

Keyword Argument

These functions are related to the function calls. Here, the function caller identifies the given argument by its parameter name. In this type of argument we get the value we stored in the argument and is used as a keyword. Let's take an example of a Keyword Argument:-

As you can clearly see that we got our output which we passed in the argument.

Default Argument

The argument which assumes a default value if a value is not given or provided in the function call for that given argument then is known as the Default Argument. Let's understand an example of the Default Argument:-

Here in the above code, we got the default age when the function is not passed.

Variable-Length Argument

These arguments and are not named in the function definition, unlike required and default arguments. These variables act as args and are denoted by an asterisk(*) and it is placed before the variable name that holds the values of all non keyword variable arguments. Let's take an example of args or Variable-length argument:-

So we got the output even after calling the args value.

Thanks for reading. Hope you guys liked it. Make sure to subscribe to our newsletter. Many more to come in the future.

NOTE:- The amazing artwork used in this article is created by:- catalyst.

Get the latest episodes directly in your inbox