Values are passed through the function. It is known as Parameters. There has a slight difference between argument and parameter. Parameters are variables, but arguments are the values of those variables. We can pass one or more variables as parameters, but we have to separate all the parameters by using a comma. In the below example, we want to print three lines like Hello Python, Hello Java, Hello World. We will pass three arguments (Python, Java, and World) individually and print the result.
def func(arg):
print("Hello " + arg)
func("Python")
func("Java")
func("World")
# Output:
# Hello Python
# Hello Java
# Hello World
We can pass as many arguments as we want. For passing two arguments, we have to call the function by passing exactly two arguments. In the below code, we will pass two integer type variables. The function will return the sum of those two variables.
def add(a, b):
return(a + b)
print(add(4, 5))
print(add(6, 9))
# Output:
# 9
# 15
We can pass the argument as a form of a key-value pair. Based on the key, the function will print the value of that key. An example is given below,
def key_function(a, b, c):
print("You Select " + b)
key_function(a = "Windows", b = "Linux", c = "Mac") # You select Linux
In Python, we can set the default parameter. When any parameters are not passed, it will print the default one. An example is given below,
def def_func(language = "Python"):
print("I know " + language)
def_func()
def_func("Java")
def_func("C#")
# Output:
#I know Python
#I know Java
#I know C#
Suppose we may not know how many augments need to pass, then we can simply write *
before the argument. This way function can take a collection object and print the result object accordingly. An example is given below,
def function(*item):
print("I know " + item[1])
function("Java", "Python", "C++")
# Output:
# I know Python
This same as the arbitrary argument. If we do not know how many arguments need to pass, then we have to use **
before the parameter name. This way the function can take inputs like a dictionary and work on the key-value pair. An example is given below,
def function(**item):
print("I know " + item["language2"])
function(language1="Java", language2="Python", language3="C++")
# Output:
# I know Python
To deal with the command-line interface, python delivers three types of argument: sys.argv, getopt module, and argparse module.
To manage various components of the PRE, this module is used. PLE stands for Python Runtime Environment. This module holds both the functions and variables. Here variables are maintained by the interpreter and functions are strongly linked with the interpreter. Command-line arguments are the main objective of this module. The name of the Python script is sys.argv[0]
.
In C language, we have a function called getopt()
. This module is alike to it. With the help of argument validation, the sys module (getopt) is used to prolong the division of the input sequence. This module allows a value assignment. A value assignment can be a short type or long type, but Python can handle it. The sys module is required to properly process the input data. We have to keep in mind that if we use this module, we have to remove the primary item from the arguments list.
It will be a better option if we use this module rather than the other two. The default argument value, positional arguments, defining the argument's data type, and support message are the options that are provided by this module