diff --git a/Programs/P72_PythonLambda.py b/Programs/P72_PythonLambda.py index 77b2da4..67fc881 100644 --- a/Programs/P72_PythonLambda.py +++ b/Programs/P72_PythonLambda.py @@ -10,14 +10,24 @@ # expression using these arguments. You can assign the function to a variable to give it a name. # The following example of a lambda function returns the sum of its two arguments: -myFunc = lambda x, y: x * y -# returns 6 -print(myFunc(2, 3)) +myFunc = lambda x, y: x * y -# example to find squares of all numbers from a list +print(myFunc(2, 3)) #output: 6 + +#Here we are directly creating the function and passing the arguments +print((lambda x, y: x * y)(2, 3)) #same output i.e 6 + +print(type(lambda x, y: x * y)) #Output: + +# example to find squares of all numbers of a list myList = [i for i in range(10)] + # returns square of each number -myFunc = lambda x: x * x +myFunc2 = lambda x: x * x + +squares = list(map(myFunc2, myList)) +print(squares) # Output: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] + +print(list(map(lambda x: x * x, myList))) #same as above + -squares = list(map(myFunc, myList)) -print(squares) # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]