In this blog, we will try to covers all types of functions Which is basically used to handle or improve python programming skill.
How to define function with python ?
We will covers all types, which is given below, functions:
Functions as Objects
Lambda functions are anonymous functions and are convenient for data analysis, as data transformation functions take functions as arguments.
Here we explain it by using Sequence of String which is followed by numbers:
string = ['nav', 'sa', 'dave']
string.sort(key = lambda x: len(list(x)))
Sort by number of letter
['sa', 'nav', 'dave']
Closures are dynamically-generated functions returned by another function. The returned function has access to the variables in the local namespace where it was created.
# This is the outer enclosing function
# This is the nested function
# We execute the function
# Output: Hi
*args and **kwargs are useful when you don't know how many arguments might be passed to your function
To read example go to the following link
Currying means to derive new functions from existing ones by partial argument application.
def add_numbers(x, y):
return x + y
add_seven = lambda y: add_numbers(7, y)
A generator is a simple way to construct a new iterable object. Generators return a sequence lazily. When you call the generator, no code is immediately executed until you request elements from the generator.
for x in xrange(1, n + 1):
yield x ** 3
# No code is executed
gen = cubes()
# Generator returns values lazily
for x in cubes():
We can write this in within few line of codes:
gen = (x ** 3 for x in xrange(1, 3))
for x in gen:
The library itertools has a collection of generators useful for data analysis.
First import it by using this:
first_letter = lambda x: x
strings = ['nav', 'dave', 'good', 'naveen']
for letter, gen_names in itertools.groupby(strings, first_letter):
print letter, list(gen_names)
It returns first character using groupby
To read other python related blog go to this python blog link
If you like Codersarts blog and looking for Assignment help,Project help, Programming tutors help and suggestion you can send mail at email@example.com.
Please write your suggestion in comment section below if you find anything incorrect in this blog post