Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5893eca

Browse files
Update P72_PythonLambda.py (#41)
Added some lines to demonstrate the use of lambda function in a more compact way. for example directly passing the arguments to lambda function.
1 parent 1b30337 commit 5893eca

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

‎Programs/P72_PythonLambda.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,24 @@
1010
# expression using these arguments. You can assign the function to a variable to give it a name.
1111
# The following example of a lambda function returns the sum of its two arguments:
1212

13-
myFunc = lambda x, y: x * y
14-
# returns 6
15-
print(myFunc(2, 3))
13+
myFunc = lambda x, y: x * y
1614

17-
# example to find squares of all numbers from a list
15+
print(myFunc(2, 3)) #output: 6
16+
17+
#Here we are directly creating the function and passing the arguments
18+
print((lambda x, y: x * y)(2, 3)) #same output i.e 6
19+
20+
print(type(lambda x, y: x * y)) #Output: <class 'function'>
21+
22+
# example to find squares of all numbers of a list
1823
myList = [i for i in range(10)]
24+
1925
# returns square of each number
20-
myFunc = lambda x: x * x
26+
myFunc2 = lambda x: x * x
27+
28+
squares = list(map(myFunc2, myList))
29+
print(squares) # Output: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
30+
31+
print(list(map(lambda x: x * x, myList))) #same as above
32+
2133

22-
squares = list(map(myFunc, myList))
23-
print(squares) # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /