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 [ ]:
 
In [ ]:
jovian.commit()
[jovian] Attempting to save notebook..
In [ ]: