-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path12_Lambda.py
More file actions
58 lines (39 loc) · 1019 Bytes
/
12_Lambda.py
File metadata and controls
58 lines (39 loc) · 1019 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
s1 = 'GeeksforGeeks'
s2 = lambda func: func.upper()
print(s2(s1))
# Example: Check if a number is positive, negative, or zero
n = lambda x: "Positive" if x > 0 else "Negative" if x < 0 else "Zero"
print(n(5))
print(n(-3))
print(n(0))
# Using lambda
sq = lambda x: x ** 2
print(sq(3))
# Using def
def sqdef(x):
return x ** 2
print(sqdef(3))
li = [lambda arg = x : arg * 10 for x in range(1,6)]
for i in li:
print(i())
# Example: Check if a number is even or odd
check = lambda x: "Even" if x % 2 == 0 else "Odd"
print(check(4))
print(check(7))
# Lambda with Multiple Statements
calc = lambda x, y: (x+y,x*y)
print(calc(5, 3))
# Using lambda with filter()
n = [1,2,3,4,5,6]
even = filter(lambda x : x % 2 == 0, n)
print(list(even))
# Using lambda with map()
a = [1,2,3,4]
b = map(lambda x: x * 2,a)
print(list(b))
# Using lambda with reduce()
from functools import reduce
# Example: Find the product of all numbers in a list
a = [1, 2, 3, 4]
b = reduce(lambda x, y: x * y, a)
print(b)