lambda

In [7]:
#lambda
val = lambda x:x*10
val(5)
Out[7]:
50
In [2]:
sum = lambda x,y:x+y
sum(3,4)
Out[2]:
7
In [3]:
def sum(x,y):
    return(x+y)
sum(3,4)
Out[3]:
7
In [6]:
f=lambda x:(9/5)*x+32
f(50)
Out[6]:
122.0
In [12]:
# map
def fahrenheit(T):
    return ((float(9)/5)*T + 32)

def celsius(T):
    return (float(5)/9)*(T-32)

temperatures = (36.5, 37, 37.5, 38, 39)
F = list(map(fahrenheit, temperatures))
print(F)

C = list(map(celsius, F))
print(C)
[97.7, 98.60000000000001, 99.5, 100.4, 102.2]
[36.5, 37.00000000000001, 37.5, 38.00000000000001, 39.0]
In [23]:
C = [36.5, 37, 37.5, 38, 39]
F = list(map(lambda x:(9/5)*x+32,C))
print(F)
C = list(map(lambda x:(5/9)*(x-32),F))
print(C)
[97.7, 98.60000000000001, 99.5, 100.4, 102.2]
[36.5, 37.00000000000001, 37.5, 38.00000000000001, 39.0]
In [26]:
# filter (filers on True)
fibonacci = [0,1,1,2,3,5,8,13,21,34,55]
odd_numbers = list(filter(lambda x: x % 2, fibonacci))
print(odd_numbers)

even_numbers = list(filter(lambda x: x % 2 == 0, fibonacci))
print(even_numbers)

even_numbers = list(filter(lambda x: x % 2 -1, fibonacci))
print(even_numbers)
[1, 1, 3, 5, 13, 21, 55]
[0, 2, 8, 34]
[0, 2, 8, 34]
www.000webhost.com