Function has a return value , And only one sentence of code lambda simplify !
There is the ultimate simplification at the end of the text , If you are interested, you can understand !
lambda parameter list : expression
notes : Parameter list is optional
1. With no arguments
fn = lambda: 100
print(fn())
# output 100
print(fn)
#output Function address
2. With parameters
fn = lambda a, b: a + b
print(fn(2, 3))
# output 5
3. Default parameters
fn = lambda a, b, c=100: a + b + c
print(fn(2, 4, 4))
# output 10
print(fn(2, 4))
# output 106
4. Use for judgment ( Used with the trinocular operator )
fn = lambda a, b: a if a > b else b
print(fn(4, 10))
# output 10
5. For unpacking
fn = lambda *args: args
print(fn(10, 20, 30))
# output (10, 20, 30)
6. The ultimate use
One line fix 1-100 Cumulative sum :
import functools
print(functools.reduce(lambda a, b: a + b, range(1, 101)))
# 5050
One line fix 1-100 Even and :
import functools
print(functools.reduce(lambda a, b: a + b, list(filter((lambda x: x % 2 == 0), range(101)))))
# 2550
It's over , Other usages are not described here .