Advance Python

Define class

class variables

Self is necessary for method signature and instance variable: like self.instanceVaraible

Explicit init constructor is not need


In [7]:
class Person:
    ##SCOPE OF CLASS DOWN BELOW
    institute="IIT"
    def __init__(self,name,department):
        self.name=name
        self.department=department
    def getName(self):
        return self.name

In [5]:
p=Person("mangesh","physics")

In [6]:
p.getName()


Out[6]:
'mangesh'

MAP FUCNTION

Some functional programming operations

map(function,interables,iterables,iterables..........)


In [8]:
help(map)


Help on class map in module builtins:

class map(object)
 |  map(func, *iterables) --> map object
 |  
 |  Make an iterator that computes the function using arguments from
 |  each of the iterables.  Stops when the shortest iterable is exhausted.
 |  
 |  Methods defined here:
 |  
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |  
 |  __iter__(self, /)
 |      Implement iter(self).
 |  
 |  __new__(*args, **kwargs) from builtins.type
 |      Create and return a new object.  See help(type) for accurate signature.
 |  
 |  __next__(self, /)
 |      Implement next(self).
 |  
 |  __reduce__(...)
 |      Return state information for pickling.


In [12]:
store1=[10,12,8,3,5]
store2=[5,8,12,3,6]
cheapest=map(min,store1,store2)
cheapest


Out[12]:
<map at 0x7f04150c1588>

We get lazy evaluation here and so map is not computed here so we get just the map object back


In [13]:
for c in cheapest:
    print(c)


5
8
8
3
5

In [15]:
people = ['Chu. Mangesh Joshi', 'Chu. Harsh Ranjan', 'Dr. Manoj Pawar', 'Dr. Ashish Shettywar']

def split_title_and_name(person):
    return person.split(" ")[0]+person.split(" ")[-1]

list(map(split_title_and_name,people))


Out[15]:
['Chu.Joshi', 'Chu.Ranjan', 'Dr.Pawar', 'Dr.Shettywar']

Lambda : Anonymous Functions

Lambda should not used for complex functions, however very handy for writing simple functions instead of defining funvtions


In [16]:
my_func=lambda a,b:a*b

a=my_func(2,3)
a


Out[16]:
6

In [20]:
for p in people:
    print( (lambda person:split_title_and_name(person))(p) )


Chu.Joshi
Chu.Ranjan
Dr.Pawar
Dr.Shettywar

In [22]:
list(map(split_title_and_name, people)) == list(map( (lambda person:person.split()[0] + person.split()[-1]),people))


Out[22]:
True

Example of list comprehension

Suppose we want to get a list of even numbers between 1-1000 there are various ways of doing it,shoiwng two ways, one with normal loops and other with list comprehension


In [27]:
##Method 1 
#Taking range as 10 to avoid a lot of printing
l=[]
for i in range(1,10):
    if i%2==0:
        l.append(i)
print(l)


[2, 4, 6, 8]

In [30]:
#Method 2
## format is [ i for expressions [conditionals]]
print([ i for i in range(1,10) if i%2==0])


[2, 4, 6, 8]

In [35]:
## convert this function list comprehensions:
def times_tables():
    lst = []
    for i in range(10):
        for j in range (10):
            lst.append(i*j)
    return lst


l=[i*j for i in range(10) for j in range(10)]

In [ ]: