1Lamda is just one line anonymous function
2Useful when writing function inside function
3it can take multiple arguments but computes only one expression
4
5Syntax:
6 x = lambda arguments : expression
1Table = lambda Table_of,Multiply_by: (Table_of*Multiply_by)
2print("3 * 9 = ",Table(3,9))
3
4def Concatinate(first_name, last_name):
5 return first_name + " " + last_name
6print("Concatinate using functions your name is:- {}".format(Concatinate("Harit","rai")))
1# This is a normal function:
2def Function(Parameter):
3 return Parameter
4
5# And this is a lambda function
6Function = lambda Parameter : Parameter
7
8"""
9
10They are both equivalent and do the exact same job (which is
11to take in a parameter and output it, in this scenario) the reason
12lambda functions exist is to make code shorter and readable since
13a lambda function only takes up one line.
14
15Lambda functions are mostly used for simple things
16Whereas defining functions are used for complex things.
17
18You do not have to use lambda functions, it's all about preference.
19
20An example of where it would be used is in basic arithmetics, im only
21going to show addition, I think you can work out the rest:
22
23"""
24
25Add = lambda a, b: a + b
26
27print(Add(3,4))
28# Output:
29# >>> 7
30
31# Its equivalent:
32
33def Add(a ,b):
34 return a + b
35
36print(Add(3,4))
37# Output:
38# >>> 7
1In Python, an anonymous function is a function that is defined without a name.
2
3While normal functions are defined using the def keyword in Python,
4anonymous functions are defined using the lambda keyword.
5
6Hence, anonymous functions are also called lambda functions.
7
8# syntax of Lambda function
9lambda arguments: expression
10
11# Program to show the use of lambda functions
12double = lambda x: x * 2
13
14print(double(5))
15
16# examples
17my_list = range(101)
18
19print(list(map(lambda item: item**2, my_list)))
20print(list(filter(lambda item: item % 2 == 1, my_list)))
21print(list(map(lambda item: item**2, my_list)))
22
23a = [(0, 2), (4, 3), (9, 9), (10, -1)]
24
25a.sort(key=lambda x: x[1])
26
27print(a)
28
29# docs lambda function
30# the lambda function can be used as a function inside a function like the following
31
32
33def make_incrementor(n):
34 return lambda x: x + n
35
36
37f = make_incrementor(12)
38print(f(3))
39print(f(13))
40
41# you can use lambda as a key for sort or anything else
42pairs = [(1, "one"), (2, "two"), (3, "three"), (4, "four")]
43# what if you wanna sort the list by the second argument
44pairs.sort(key=lambda x: x[1])
45print(pairs)
46
47
1In Python, an anonymous function is a function that is defined without a name.
2
3While normal functions are defined using the def keyword in Python,
4anonymous functions are defined using the lambda keyword.
5
6Hence, anonymous functions are also called lambda functions.
7
8# syntax of Lambda function
9lambda arguments: expression
10
11# Program to show the use of lambda functions
12double = lambda x: x * 2
13
14print(double(5))