Lambda Practice

In [1]:
sum = lambda x, y : x + y
sum(3,4)
Out[1]:
7
In [3]:
# in this example T represents temperature, C celsius and F fahrenheit.  

def fahrenheit(T): # function to calculate fahrenheit
    return ((float(9)/5)*T + 32)

def celsius(T): # function to calculate celsius
    return (float(5)/9)*(T-32)

temperatures = (36.5, 37, 37.5, 38, 39) # a list called temperatures
F = map(fahrenheit, temperatures) # the map function takes the two variables for fahrenheit and sores them in F
C = map(celsius, F) 

temperatures_in_fahrenheit = list(map(fahrenheit, temperatures)) # then make a list of all the calculations
temperatures_in_celsius = list(map(celsius, temperatures_in_fahrenheit))

print(temperatures_in_fahrenheit) # print the list
[97.7, 98.60000000000001, 99.5, 100.4, 102.2]
In [4]:
temperatures_in_celsius
Out[4]:
[36.5, 37.00000000000001, 37.5, 38.00000000000001, 39.0]
In [5]:
# in this example lambda is used instead of functions

C = [39.2, 36.5, 37.3, 38, 37.8]
F = list(map(lambda x: (float(9)/5)*x + 32, C))

print(F)
[102.56, 97.7, 99.14, 100.4, 100.03999999999999]
In [7]:
C = list(map(lambda x: (float(5)/9)*(x-32), F))

print(C)
[39.2, 36.5, 37.300000000000004, 38.00000000000001, 37.8]
In [8]:
a = [1, 2, 3, 4]
b = [17, 12, 11, 10]
c = [-1, -4, 5, 9]

list(map(lambda x, y : x+y, a, b))
Out[8]:
[18, 14, 14, 14]
In [9]:
list(map(lambda x, y, z : x+y+z, a, b, c))
Out[9]:
[17, 10, 19, 23]
In [10]:
list(map(lambda x, y, z : 2.5*x + 2*y - z, a, b, c))
Out[10]:
[37.5, 33.0, 24.5, 21.0]
In [11]:
a = [1, 2, 3]
b = [17, 12, 11, 10]
c = [-1, -4, 5, 9]

list(map(lambda x, y, z : 2.5*x + 2*y - z, a, b, c))
Out[11]:
[37.5, 33.0, 24.5]
In [ ]: