Python is a high level general purpose programming language which has a clear/easy learning curve. Python programming language is massively used in various domains like Artificial Intelligence, Data Science, Web Development, Utilities Tools and Scripts and many more
Python is a Programming language, and the default Python we use is written in C programming language which is also referred to as CPython (Python implementation in C). There are various implementation of Python Programming language i.e Jython(in JAVA), Skulpt(in JS) e.t.c
To make everything easy: We refer CPython as Python
Arguments are those values or data which are passed to the function in order to be accessed by that particular function. Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma.
Types of arguments in python are:
These arguments are also called positional arguments in which the number of arguments in the function call
should be
equal to the number of arguments in the function definition.
The position of the arguments in the function call and function definition should be matched otherwise
function
would give unwanted results.
#funtion that prints the info of age and name of a person
def printinfo(name,age):
print("My name: {}.\nMy age{}.".format(name,age))
myname=input("Enter your name: ")
myage=int(input("Enter your age: ")
printinfo(myname,myage)
Output:
Enter your name: Tom
Enter your age: 23
My name: Tom
My age: 23
These arguments make a function call more efficient and flexible. Keyword arguments help the caller function to identify the related parameter by its name. So, we don't need to take care of the position of the arguments. In a function call, we pass arguments using the same parameter name in the function definition
Example:
#funtion that prints the info of age and name of a person
def printinfo(name,age):
print("My name: {}.\nMy age{}.".format(name,age))
printinfo(age=20,name="Sam") #in this function call position of argument doesn't matter
Output:
My name: Sam
My age: 20
Default arguments
In the function definition, we assume a default value for an argument. In the function call, if we pass a value in the function call the default value of an argument will be overwritten otherwise default value will be used
Example:
#funtion that prints the info of age and name of a person.
def printinfo(name,age=20):
print("My name: {}.\nMy age{}.".format(name,age))
printinfo(name="Harry",age=30) #passed a value to the default argument.
printinfo(name="Harry") #default argument(age) not passed.
In some cases, We may not know the exact number of arguments that should be passed to a function. In this case, python allows us to use some special functionality of argument in the definition of a function that is what is known as Arbitrary arguments.
In the function definition, we use an asterisk (*) before the parameter name to denote this kind of argument
Example:
def findsum(*numbers):
sum=0
for number in numbers:
sum+=number
return sum
total=findsum(2,3,4,5,6)
total1=findsum(1,2,32,3,22,5)
print(total)
print(total1)
Output:
20
65
Arbitrary arguments are also called variable-length arguments because we can pass a variable number of arguments to the function.
There are two types of arbitrary/variable-length arguments
In arbitrary positional arguments(*args), an asterisk(*) is placed before the parameter name in order to pass a variable number of non-keyworded arguments to a function. Here, all the arguments are passed to the function as a tuple.
The main goal of *args in python is to pass the variable number of non-keyworded arguments to a function.
Example
def numbers(*args):
print(args)
print(type(args))
numbers(1,2,3)
numbers(1,2,3,4,5)
Output:
(1, 2, 3)
<class 'tuple'>
(1, 2, 3,4,5)
<class 'tuple'>
Now, the argument with the * sign is a tuple. so now we can iterate over it
Example
def numbers(*args):
for number in args:
print(number,end="\t")
numbers(1,2,3,4,5)
Output:
1 2 3 4 5
Example of *args
Let us define a function that is used to calculate the average of numbers with multiple lengths of arguments support.
def calculate_average(*args):
print(sum(args)/len(args))
calculate_average(1,2)
calculate_average(1,2,3)
calculate_average(1,2,3,4)
Output:
1.5
2.0
2.5
In arbitrary positional arguments(**kwargs), an asterisk(**) is placed before the parameter name in order to pass a variable number of keyworded arguments to a function. Here, all the arguments are passed to the function as a dictionary
The main goal of *kwargs in python is to pass the variable number of keyworded arguments to a function
Example
def info(**kwargs):
print(kwargs)
print(type(kwargs))
info(Name="Jaffries",age=24,Address="Utah, USA")
Output:
Name : Jaffries
age : 24
Address : Utah, USA
If we pass keyworded and non-keyworded arguments to a function with *args and **kwargs, arguments are assigned to the corresponding variables.
Example
def arguments(*args,**kwargs):
print(args)
print(kwargs)
arguments(1,2,3,"Hello",Course="BIT",Duration="4 Years",University="Harvard University")
Output:
(1, 2, 3, 'Hello')
{'Course': 'BIT', 'Duration': '4 Years', 'University': 'Harvard University'}