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 a25ec62

Browse files
Enhanced version
1 parent f31e2a1 commit a25ec62

File tree

1 file changed

+28
-67
lines changed

1 file changed

+28
-67
lines changed

‎Lambda/WhatIsLambda.py

Lines changed: 28 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,38 @@
1+
"""
2+
What is a lambda function in Python?
13
4+
A lambda function is a small anonymous function defined with the 'lambda' keyword.
5+
It can take any number of arguments but can only have one expression.
26
3-
def squareNumber(n):
4-
if type(n) == int:
5-
return n*n
6-
else:
7-
return 0
7+
Syntax:
8+
lambda arguments: expression
9+
"""
810

9-
def checkForEven(n):
10-
if type(n) is not int:
11-
return
12-
return n%2 ==0
11+
# Example 1: Basic lambda function
12+
add = lambda x, y: x + y
13+
print("add(2, 3):", add(2, 3)) # Output: 5
1314

14-
def add(x, y):
15-
return x+y
16-
print(squareNumber(10))
15+
# Example 2: Lambda with map
16+
numbers = [1, 2, 3, 4]
17+
squared = list(map(lambda x: x ** 2, numbers))
18+
print("Squared numbers:", squared) # Output: [1, 4, 9, 16]
1719

18-
# if you want to pass a list as a parameter, then will the above function (add()) block will support?
20+
# Example 3: Lambda with filter
21+
evens = list(filter(lambda x: x % 2 == 0, numbers))
22+
print("Even numbers:", evens) # Output: [2, 4]
1923

20-
# NO
24+
# Example 4: Lambda with sorted
25+
pairs = [(2, 3), (1, 4), (5, 0)]
26+
sorted_pairs = sorted(pairs, key=lambda pair: pair[1])
27+
print("Pairs sorted by second element:", sorted_pairs) # Output: [(5, 0), (2, 3), (1, 4)]
2128

22-
sampleList =[1,2,3,4,5]
23-
outputList = []
24-
for i in sampleList:
25-
outputList.append(squareNumber(i))
29+
# Comparison: Regular function vs lambda
30+
def multiply(x, y):
31+
return x * y
2632

27-
print(outputList)
33+
multiply_lambda=lambdax, y: x*y
2834

35+
print("multiply(3, 4):", multiply(3, 4)) # Output: 12
36+
print("multiply_lambda(3, 4):", multiply_lambda(3, 4)) # Output: 12
2937

30-
outputList2 = []
31-
for i in sampleList:
32-
if checkForEven(i):
33-
outputList2.append(i)
34-
print("output list 2 is ", outputList2)
35-
# ===================================================
36-
37-
evenNum = list(filter(lambda x: x%2 ==0, sampleList))
38-
print(evenNum)
39-
40-
41-
sqNum = list(map(squareNumber, sampleList))
42-
print("sqNum is .. ", sqNum)
43-
44-
globalExample = map(lambda x, y: (x+y, x-y, x*y), [10, 20, 30, 40],[5,10,15,20])
45-
globalExample = map(lambda x, y: (x+y, x-y, x*y), [10, 20, 30, 40],[5,10,15,20])
46-
print(list(globalExample))
47-
48-
49-
50-
51-
52-
# List of strings
53-
l = ['sat', 'bat', 'cat', 'mat']
54-
55-
opList = []
56-
for i in l:
57-
opList.append(list(i))
58-
print(opList)
59-
60-
map(list, l)
61-
62-
63-
64-
# class calculator:
65-
# add = lambda x,y: x+y
66-
# sub = lambda x,y: x-y
67-
# mul = lambda x,y: x*y
68-
69-
# def add(a,b):
70-
# return a+b
71-
72-
# print("here you go ...")
73-
74-
# cal = calculator()
75-
76-
# print(calculator.add(10,5))
77-
38+
# Note: Lambda functions are best for short, simple operations.

0 commit comments

Comments
(0)

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