Parameters are defined by the names that appear in a function definition, whereas arguments are the values actually passed to a function when calling it. Parameters define what types of arguments a function can accept. For example, given the function definition:
def func(foo, bar=None, **kwargs):
pass
#foo, bar and kwargs are parameters of func. However, when calling func, for example:
func(42, bar=314, extra=somevar)
#the values 42, 314, and somevar are arguments.
The syntax is the somename and **somename. The names args and *kwargs are only by convention but there's no strict requirement to use them.
You would use args when you're not sure how many arguments might be passed to your function. It allows you pass an arbitrary number of arguments to your function.
def addnum(*args):
a = 0
for n in args:
a += num
print(a)
addnum(4, 9) # 13
addnum(10, 7) # 17
addnum(2, 7, 4) # 13
addnum(3, 8, 10, 2) # 23
With **kwargs you can give arbitrary keyword arguments to a function and you can access them as a dictionary.
def func1(**kwargs):
for key, value in kwargs.items():
print(key, value)
func1(name='jack', animal= ‘monkey’ , eyes=2)
# name jack
# animal monkey
# eyes 2
Using args and *kwargs in function call
def func1(x,y,z):
print(x,y,z)
l1 = [5,6,8]
fun1(*l1) # 5 6 8
d1 = {1: ‘first’, ‘s’: ‘second’, ‘m’: ‘third’}
fun1(*t1) # first second third
It is a built-in function of python. It takes an iterable object as an argument and returns an enumerate object.
Syntax: enumerate(iterable, start=0)
Example:
We can get a list of tuples from Enumerate object by built-in function list().
fruits = ['apple', 'orange', 'banana', 'grapes']
print(list(enumerate(fruits)))
[(0, 'apple'), (1, 'orange'), (2, 'banana'), (3, 'grapes')]
By default indexing starts with “0”, but we can specify the starting index.
fruits = ['apple', 'orange', 'banana', 'grapes']
print( list(enumerate(fruits, start = 10)))
[(10, 'apple'), (11, 'orange'), (12, 'banana'), (13, 'grapes')]
Using of Enumerate with for loop
fruits = ['apple', 'orange', 'banana', 'grapes']
for index, value in enumerate(fruits, 1):
print(index, value)
# 1 apple
# 2 orange
# 3 banana
# 4 grapes
Small anonymous functions can be created with the lambda keyword. Lambda functions can be used wherever function objects are required. They are syntactically restricted to a single expression. Semantically, they are just syntactic sugar for a normal function definition. Like nested function definitions, lambda functions can reference variables from the containing scope
These functions return the sum of its two arguments
func1 = lambda a, b: a+b
def func2(a,b):
return a+b
func1(2,3) # 5
func2(3,4) # 7
A string literal which appears as the first expression in a class, function or module. While ignored when the suite is executed, it is recognized by the compiler and put into the doc attribute of the enclosing class, function or module. This documentation string can be written inside single quote, double quote or triple quote.
>>> def myfunction():
... """This is the documentation for myfunction"""
... pass
...
>>> myfunction.__doc__
'This is the documentation for myfunction'
zip(*iterables) makes an iterator that aggregates elements from each of the iterables.
It returns an iterator of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables.
x = [1, 2, 3]
y = [4, 5, 6]
z = ‘abc’
zip1 = zip(x, y)
zip2 = zip(y,z)
print(list(zip1))
print(list(zip2))
Output
The iterator stops when the shortest input iterable is exhausted
>>> A = zip('ABCD', 'xy')
>>> print(list(A))
[('A', 'x'), ('B', 'y')]
It will return iterator tuples with single element.
>>> a=zip('asdf')
>>> for element in a:
... print(element)
...
('a',)
('s',)
('d',)
('f',)
map(function, iterable, ...) takes its 1st argument a function, and then any number of iterable object like list, tuple, dictionary etc. It returns an iterator that applies the function to every item of the iterable.
X = map(lambda x : x*2, [1, 2, 3, 4])
print(list(X))
Output
Like map function filter(function, iterable, ...) takes its 1st argument a function, and then any number of iterable object like list, tuple, dictionary etc. But it returns an iterator which have only those elements for which the function returns true.
X = filter(lambda x : x%2!=0, [1, 2, 3, 4, 5, 6, 7, 8])
print(list(X))
Output
reduce(function, iterable[, initializer]) takes a function as its first argument and iterable as compulsory argument and initializer as optional argument.
It applies the function from left to right to reduce the iterable into single element.
X = reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])
# calculates ((((1+2)+3)+4)+5)
print(X)
Output
A value passed to a function (or method) when calling the function.
There are two kinds of argument:
keyword argument:
An argument preceded by an identifier (e.g. name=) in a function call or passed as a value in a dictionary preceded by **.
For example:
3 and 5 are both keyword arguments in the following calls to complex()
complex(real=3, imag=5)
complex(**{'real': 3, 'imag': 5})
Positional argument:
An argument that is not a keyword argument.
Positional arguments can appear at the beginning of an argument list and/or be passed as elements of an iterable preceded by *.
For example:
3 and 5 are both positional arguments in the following calls
complex(3, 5)
complex(*(3, 5))