-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Expand file tree
/
Copy pathlambda_func.py
More file actions
27 lines (16 loc) · 776 Bytes
/
lambda_func.py
File metadata and controls
27 lines (16 loc) · 776 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Lambda functions are called anonymous functions because they don't have a name,
# and they are not defined with the def keyword. Instead,
# they are defined using the lambda keyword followed by a list of arguments,
# a colon, and an expression that is evaluated and returned as the result of the function.
# Example 1
# this is a simplest example of a lambda function that inputs an integer and returns its square
unknownSquarer_func = lambda x: x*2
print(unknownSquarer_func(2)) # --> 4
# Example 2
# iterable list
numList = [1,2,3,4,5]
# this lambda function is the function that will be applied to each item in iterable using map keyword
squaredNumbers = map(lambda x:x*2,numList)
print(list(squaredNumbers))