| Download
Worksheets related to Applied Discrete Structures
Project: Applied Discrete Structures
Views: 15776Image: ubuntu2004
Kernel: SageMath (stable)
Lambda functions
Lambda functions are often used in map
, filter
, and sort
. They are analogous to Mathematica pure functions. A pure function such as {#,#^2}&
in Mathematica would be the lambda function lambda k:[k,k^2]
in Python/SageMath.
In [1]:
In [2]:
<function <lambda> at 0x7f7d8f6a60c8>
In [3]:
[5, 25]
In [4]:
[a^2 + 1, (a^2 + 1)^2]
In [5]:
[[0, 0],
[1, 1],
[2, 4],
[3, 9],
[4, 16],
[5, 25],
[6, 36],
[7, 49],
[8, 64],
[9, 81]]
The list [a,b]
and the tuple (a,b)
are different. However, either can be used in plotting.
In [6]:
[(0, 0),
(1, 1),
(4, 2),
(9, 3),
(16, 4),
(25, 5),
(36, 6),
(49, 7),
(64, 8),
(81, 9)]
In [7]:
In [8]:
[2,
3,
5,
7,
11,
13,
17,
19,
23,
29,
31,
37,
41,
43,
47,
53,
59,
61,
67,
71,
73,
79,
83,
89,
97]
In [29]:
[2,
3,
5,
7,
11,
13,
17,
19,
23,
29,
31,
37,
41,
43,
47,
53,
59,
61,
67,
71,
73,
79,
83,
89,
97]
In [9]:
In [10]:
[717, 623, 641, 741, 527, 791, 899, 1, 555, 522]
The following expression doesn't produce an output but does act on the list.
In [11]:
In [12]:
[1, 522, 527, 555, 623, 641, 717, 741, 791, 899]
In [16]:
In [17]:
[899, 527, 717, 555, 623, 522, 1, 641, 741, 791]
In [0]: