Repository navigation
Expand file tree
/
Copy pathpython-lambda.py
More file actions
129 lines (102 loc) · 3.41 KB
/
Copy pathpython-lambda.py
File metadata and controls
129 lines (102 loc) · 3.41 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
""" MARKDOWN
---
YamlDesc: CONTENT-ARTICLE
Title: python lambda Map Filter Reduce
MetaDescription: python lambda, Map, Filter, Reduce, code, tutorials
MetaKeywords: python lambda, Map, Filter, Reduce, code, tutorials
Author: Venkata Bhattaram / tinitiate.com
ContentName: python-lambda
---
MARKDOWN """
""" MARKDOWN
# Python Lambda Expressions
* Lambdas Expressions or Lambdas are one liner syntax to create
anonymous functions called Lambdas, They allow functionality and
data to be passed around in a single line.
* They provide a shortcut to expressions, LOOP and IF..ELSE conditions.
MARKDOWN """
""" MARKDOWN
## Python Lambda Demonstration
* Below example demonstrates Normal Function and its `lambda` equvalent
MARKDOWN """
# MARKDOWN ```
# NORMAL FUNCTION
# Function to Add 2 Numbers
def Add2Nums(Num1, Num2):
return Num1+Num2;
# LAMBDA FUNCTION
# Function to Add 2 Numbers
AddLda = lambda x,y: x+y
print(AddLda(1,3))
# MARKDOWN ```
""" MARKDOWN
* Below example demonstrates `lambda` with IF..ELSE condition
MARKDOWN """
# MARKDOWN ```
# NORMAL FUNCTION With IF..ELSE
# Function Identify if EVEN or ODD
def EvenOrOdd(Num1):
if(Num1%2 == 0):
return "EVEN";
else:
return "ODD";
# LAMBDA FUNCTION
# Lambda Identify if EVEN or ODD
EvenOrOddLda = lambda x: 'EVEN' if(x%2==0) else 'ODD'
print(EvenOrOddLda(12))
# MARKDOWN ```
""" MARKDOWN
## Python Map Function with Lambda
* Map function applies or MAPS a single or multiple Functions sequentialy
to each element in a source iteration from (list, dictionary, tuples)
* It executes the function_object for each element in the source iteration
and returns a list of the elements modified by the source function(s)
* USAGE: map(someFunction, someSequence)
MARKDOWN """
# MARKDOWN ```
# MAP Single Function with an Iteration with Lambda
sequence = [2, 3, 4, 5, 6]
y = list(map(lambda v : v * 5, sequence))
print(y)
# MAP Multiple Functions with an Iteration with Lambda
def Add2(x):
return (x+2)
def Add3(x):
return (x+3)
MyFunctions = [Add2, Add3]
for num in range(5):
data = list(map(lambda x: x(num), MyFunctions))
print(data)
# MARKDOWN ```
""" MARKDOWN
## Python Filter Function Lambda Expressions
* Filter function works with two arguments a Function and Source iterable.
* The function is called for each element of the iterable with filter
returning only elements which qualify for a given condition.
* Unlike map function filter function can only have one iterable as input.
MARKDOWN """
# MARKDOWN ```
# FILTER Function
# Removes list of a values that dont match the criteria
# Create a List of Even Numbers Only
evenList = list( filter((lambda x: x%2 == 0), range(10)))
print(evenList)
# Create a List of Salary only > 1000
salary_list = [100, 2000, 300, 4000]
list_1000_PLUS = list( filter((lambda x: x>1000), salary_list))
print(list_1000_PLUS)
# MARKDOWN ```
""" MARKDOWN
## Python Reduce Function with Lambda Expressions
* REDUCE Function
* Import Reduce from functools in Python 3X
* Accepts a function and list of values to return a single value
* by processing the result over and over untill a single value remains
MARKDOWN """
# MARKDOWN ```
from functools import reduce
addData = reduce((lambda x,y:x+y),range(10))
print(addData)
mulData = reduce((lambda x,y:x*y),range(1,10))
print(mulData)
# MARKDOWN ```