Jovian
⭐️
Sign In
In [1]:
import jovian
jovian.commit()
[jovian] Attempting to save notebook.. [jovian] Uploading notebook.. [jovian] Capturing environment.. [jovian] Committed successfully! https://jovian.ml/rajan-chettri01/ds-c29-week3-slot1
In [ ]:
 
In [2]:
#perspective 

#Comprehensions | short cute sweet elegant expressions

#one liner codes
# to improve code readability
# DO MORE WITH LESS LINES OF CODE.

In [ ]:
# lIST COMPREHENSION

# Data -> logic -> List


In [4]:
[ 'ola' for x in range(5) ]
Out[4]:
['ola', 'ola', 'ola', 'ola', 'ola']
In [5]:
[ x for x in range(5) ]
Out[5]:
[0, 1, 2, 3, 4]
In [6]:
[ x*x for x in range(5) ]
Out[6]:
[0, 1, 4, 9, 16]
In [ ]:
# comprehensions with condition
In [9]:
data=[x for x in range(10) if x<5]
In [10]:
print(data)
[0, 1, 2, 3, 4]
In [ ]:
 
In [ ]:
# Dictionary Comprehension

# RAW Data->logic->List
# RAW Data->Logic->dictionary
In [12]:
{ x:'ola' for  x in range(5) }
Out[12]:
{0: 'ola', 1: 'ola', 2: 'ola', 3: 'ola', 4: 'ola'}
In [13]:
{ x:x*x for  x in range(5) }
Out[13]:
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
In [17]:
# Case 2

mywords=['a','aa','aaa','aaaa','aaaaa','bbb','bb','b']
In [18]:
## write a comprehension expressions to 
#produce a dictionary where
# key:word
# value: number of characters
In [20]:
{w:len(w) for w in mywords}
Out[20]:
{'a': 1, 'aa': 2, 'aaa': 3, 'aaaa': 4, 'aaaaa': 5, 'bbb': 3, 'bb': 2, 'b': 1}
In [21]:
# let us suppose we are importing some items from US in India
# the idea here is to convert the price value from $->INR

imported_items={'milk':1.05,'Coffee':2.65,'Bread':4.5}
In [ ]:
#hint: items()
In [24]:
{ item:price*72.44 for item,price in imported_items.items()}
Out[24]:
{'milk': 76.062, 'Coffee': 191.96599999999998, 'Bread': 325.98}
In [ ]:
 
In [ ]:
# Lambda Expressions
# are alternate to Python functions!!

In [26]:
def sayHello():
    print('Hello')
In [27]:
sayHello1 = lambda : 'Hello'
In [28]:
sayHello()
Hello
In [29]:
sayHello1()
Out[29]:
'Hello'
In [ ]:
 
In [33]:
def getsqr(n):
    return n*n
In [34]:
getsq = lambda n : n*n
In [35]:
getsq(5)
Out[35]:
25
In [36]:
pow = lambda x,y: x**y
In [37]:
pow(5,3)
Out[37]:
125
In [42]:
nba_players=[119,165,135,110,174]
In [ ]:
# using lambda expression 
# + comprehension try to convert the weight
# from pounds-> KG
In [40]:
ptok = lambda weight : weight*0.45
In [41]:
ptok(100)
Out[41]:
45.0
In [44]:
[ ptok(player_weight) for player_weight in nba_players]
Out[44]:
[53.550000000000004, 74.25, 60.75, 49.5, 78.3]
In [ ]:
 
In [ ]:
# Functional Programming

# Map , filter, reduce
In [ ]:
 
In [47]:
### map
In [50]:
## Data

numbers=[1,2,3,4,5]
In [51]:
# programming logic

def getsqr(n):
    return n*n

#--------

lambda  n : n*n

In [53]:
list( map ( getsqr , numbers ) )
Out[53]:
[1, 4, 9, 16, 25]
In [ ]:
 
In [54]:
list( map ( lambda  n : n*n  , numbers ) )
Out[54]:
[1, 4, 9, 16, 25]
In [ ]:
 
In [55]:
list( map ( lambda  n : n*n  , [1,2,3,4,5] ) )
Out[55]:
[1, 4, 9, 16, 25]
In [ ]:
 
In [ ]:
## Filter example
In [59]:
numbers=list(range(1,11))
In [60]:
numbers
Out[60]:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
In [ ]:
[False,True,False,True,False,True,False,True..]
In [61]:
list( filter( lambda  num: num%2==0  , numbers ))
Out[61]:
[2, 4, 6, 8, 10]
In [ ]:
 
In [62]:
colors=['red','orange','cyan','green','indigo']
In [ ]:
# use the filter to extract colors starting with vowel
In [66]:
list(filter( lambda color : color[0] in 'aeiou' ,  colors))
Out[66]:
['orange', 'indigo']
In [67]:
garbage=["hello",100,True,[10,20],200,False,'hi',300]
In [ ]:
#extract the integers from the list above
#expected output:

#100,200,300
In [69]:
list(filter(lambda item : type(item)==int,garbage))
Out[69]:
[100, 200, 300]
In [ ]:
 
In [70]:
#reduce

from functools import reduce
In [74]:
numbers=[1,2,3,4,5]
In [75]:
def getsum(x,y):
    print('x {} y {}'.format(x,y))
    return x+y
In [76]:
reduce(getsum,numbers)
x 1 y 2 x 3 y 3 x 6 y 4 x 10 y 5
Out[76]:
15
In [ ]:
# x     y
# 1     2
# 3     3
# 6     4
# 10    5
# 15
# return x
In [ ]:
## Object Oriented Programming
In [ ]:
 
In [ ]:
 

shyam is an employee and he gets salary of 5000
and his job is to approveloan

In [ ]:
## fill in the blanks

## Class:     Employee___
## Object:    _shyam__
## Property:  _salary=5000__
## function:  _approveLoan()__
In [80]:
## observe the simulation below:


class Employee: # creating class
    
    salary=5000               #creating property
    
    def approveLoan(self):    #creating function
        print('loan approved')
        self.title="software engineer"
        perks="tea,coffee"
        
shyam = Employee() # creating object
shyam.approveLoan() # calling function via object!
    
loan approved
In [ ]:
 
In [81]:
shyam.title
Out[81]:
'software engineer'
In [82]:
shyam.perks
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-82-da796ff8f18b> in <module>() ----> 1 shyam.perks AttributeError: 'Employee' object has no attribute 'perks'
In [ ]:
 
In [ ]:
 
In [84]:
class Student:
    
    score=0
    
oliver = Student()
windy = Student()
victor= Student()
In [85]:
oliver.score=10
windy.score=5

victor.score = oliver.score+windy.score
In [86]:
print('oliver ',oliver.score)
print('windy ',windy.score)
print('victor ',victor.score)
oliver 10 windy 5 victor 15
In [ ]:
#Patterns

# 1,2,3,4,5
# 1,1,1,1,1
# A,B,C,D,E

#1
#12
#123
#1234
#12345

#A
#AB
#ABC
#ABCD
#ABCDE

#54321
#5432
#543
#54
#5

#ABCDE
#ABCD
#ABC
#AB
#A



In [ ]:
 
In [ ]:
jovian.commit()
[jovian] Attempting to save notebook..
In [ ]: