Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions Programs/P72_PythonLambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -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: <class 'function'>

# 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]