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]:
Some functional programming operations
map(function,interables,iterables,iterables..........)
In [8]:
help(map)
In [12]:
store1=[10,12,8,3,5]
store2=[5,8,12,3,6]
cheapest=map(min,store1,store2)
cheapest
Out[12]:
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)
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]:
In [16]:
my_func=lambda a,b:a*b
a=my_func(2,3)
a
Out[16]:
In [20]:
for p in people:
print( (lambda person:split_title_and_name(person))(p) )
In [22]:
list(map(split_title_and_name, people)) == list(map( (lambda person:person.split()[0] + person.split()[-1]),people))
Out[22]:
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)
In [30]:
#Method 2
## format is [ i for expressions [conditionals]]
print([ i for i in range(1,10) if i%2==0])
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 [ ]: