Python Functions - Codersarts

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


  • Functions as Objects

  • Lambda Functions

  • Closures

  • *args, **kwargs

  • Currying

  • Generators

  • Generator Expressions

  • itertools


Lambda Functions

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:


Example:


string = ['nav', 'sa', 'dave']

string.sort(key = lambda x: len(list(x)))

strings


Sort by number of letter


Output:

['sa', 'nav', 'dave']



Closures Functions

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.


Example:

def print_msg(msg):

# This is the outer enclosing function

def printer():

# This is the nested function

print(msg)

printer()

# We execute the function

# Output: Hi

print_msg("Hi")


Output:


Hi


*args, **kwargs

*args and **kwargs are useful when you don't know how many arguments might be passed to your function


Example:


To read example go to the following link


Currying

Currying means to derive new functions from existing ones by partial argument application.


Example:


def add_numbers(x, y):

return x + y

add_seven = lambda y: add_numbers(7, y)

add_seven(8)


Output:


15


Generators

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.


Example:

def cubes(n=3):

for x in xrange(1, n + 1):

yield x ** 3

# No code is executed

gen = cubes()

# Generator returns values lazily

for x in cubes():

print x


Output:


1

8

27


We can write this in within few line of codes:

gen = (x ** 3 for x in xrange(1, 3))

for x in gen:

print x


Output:


1

8

27


itertools


The library itertools has a collection of generators useful for data analysis.


First import it by using this:


import itertools


Example:


import itertools

first_letter = lambda x: x[0]

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


Output:


n['nav', 'naveen']

d['dave']

g['good']


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 contact@codersarts.com.

Please write your suggestion in comment section below if you find anything incorrect in this blog post


Contact Us

Tel: (+91) 0120  4118730  

Time :   10 : 00  AM -  08 : 00 PM IST 

Registered address: G-69, Sector 63, 

 Noida - 201301, India

We Provide Services Across The different countries

USA    Australia   Canada   UK    UAE    Singapore   New Zealand    Malasia   India   Ireland   Germany

CodersArts is a Product by Sofstack Technology Solutions Pvt. Ltd.

  • CodersArts | Linkedin
  • Instagram