Arbitrary argument list
The function can be called with arbitrary number of argument this arguments will be wrapped up in a tuple before the variable number of arguments zero or more normal arguments may occur
def write_multiple_items(file, separator, *args):
file.write(separator.join(args))
file.write(separator.join(args))
Normally this variadic arguments comes last in the list of formal parameters because they scoop up all remaining input arguments that are passed to the function any formal parameters which occur after the *args are keyword only arguments means they can only be used as key words rather than positional arguments
>>> def concat(*args, sep="/"): ... return sep.join(args) ... >>> concat("earth", "mars", "venus") 'earth/mars/venus' >>> concat("earth", "mars", "venus", sep=".") 'earth.mars.venus'
UnPacking Argument lists
The argument list can be unpacked if the arguments are in a tuple or list but need to be unpacked for a function call requiring positional arguments like the range() function expects seperate start and stop arguments if they are not available separately write the function call with the * - operator to unpack the arguments out of a list or tuple
>>> list(range(3, 6)) # normal call with separate arguments [3, 4, 5] >>> args = [3, 6] >>> list(range(*args)) # call with arguments unpacked from a list [3, 4, 5]
in the same fashion dictionaries can deliver key word arguments with the **- operator:
>>> def parrot(voltage, state='a stiff', action='voom'): ... print("-- This parrot wouldn't", action, end=' ') ... print("if you put", voltage, "volts through it.", end=' ') ... print("E's", state, "!") ... >>> d = {"voltage": "four million", "state": "bleedin' demised", "action": "VOOM"} >>> parrot(**d) -- This parrot wouldn't VOOM if you put four million volts through it. E's bleedin' demised !
Lambda expressions
The small anonymous function can be created with the lambda keyword this function returns the sum of its two arguments lambda a , b : a+b Lambda function can be used wherever function objects are required they are syntactically restricted to a single expression lambda function can reference variables from the containing scope :
>>> def make_incrementor(n): ... return lambda x: x + n ... >>> f = make_incrementor(42) >>> f(0) 42 >>> f(1) 43
Other one giving above here an example of a lambda function to return a function another one given below is example to pass a small function as an argument
>>> pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')] >>> pairs.sort(key=lambda pair: pair[1]) >>> pairs [(4, 'four'), (1, 'one'), (3, 'three'), (2, 'two')]
More on Lists
here are some of the list data type methods
list.append(x)
add an item to the end of the list equivalent to a
list.append(x)
add an item to the end of the list equivalent to a
a[len(a):] = [x]
.-
list.
extend
(iterable) - Extend the list by appending all the items from the iterable. Equivalent to
a[len(a):] = iterable
.
-
list.
insert
(i, x) - Insert an item at a given position. The first argument is the index of the
element before which to insert, so
a.insert(0, x)
inserts at the front of the list, anda.insert(len(a), x)
is equivalent toa.append(x)
.
-
list.
remove
(x) - Remove the first item from the list whose value is equal to x. It raises a
ValueError
if there is no such item.
-
list.
pop
([i]) - Remove the item at the given position in the list, and return it. If no index
is specified,
a.pop()
removes and returns the last item in the list. (The square brackets around the i in the method signature denote that the parameter is optional, not that you should type square brackets at that position. You will see this notation frequently in the Python Library Reference.)
-
list.
clear
() - Remove all items from the list. Equivalent to
del a[:]
.
-
list.
index
(x[, start[, end]]) - Return zero-based index in the list of the first item whose value is equal to x.
Raises a
ValuError
if there is no such item.
The optional arguments start and end are interpreted as in the slice notation and are used to limit the search to a particular sub-sequence of the list. The returned index is computed relative to the beginning of the full sequence rather than the start argument.
-
list.
count
(x) - Return the number of times x appears in the list.
-
list.
sort
(key=None, reverse=False) - Sort the items of the list in place (the arguments can be used for sort
customization, see
sorted()
for their explanation).
-
list.
reverse
() - Reverse the elements of the list in place.
-
list.
copy
() - Return a shallow copy of the list. Equivalent to
a[:]