原文:

先决条件:

在下面的程序中,我们已经在 map()内置函数中使用了匿名(lambda)函数来求 2 的幂。在 python 中,匿名函数是在没有名称的情况下定义的。 虽然普通函数是使用 def 关键字定义的,但是在 python 中匿名函数是使用 lambda 关键字定义的。因此,匿名函数也被称为 lambda 函数。 语法:

lambda arguments: expression

lambda 函数可以有任意数量的参数,但只能有一个表达式。表达式被求值并返回 示例:

input : ('the total terms is:', 10)
output :
('2 raised to power', 0, 'is', 1)
('2 raised to power', 1, 'is', 2)
('2 raised to power', 2, 'is', 4)
('2 raised to power', 3, 'is', 8)
('2 raised to power', 4, 'is', 16)
('2 raised to power', 5, 'is', 32)
('2 raised to power', 6, 'is', 64)
('2 raised to power', 7, 'is', 128)
('2 raised to power', 8, 'is', 256)
('2 raised to power', 9, 'is', 512)
# python program to display the powers 
# of 2 using anonymous function
# change this value for a different result
terms = 10
# uncomment to take number of terms from user
# terms = int(input("how many terms? "))
# use anonymous function
result = list(map(lambda x: 2 ** x, range(terms)))
# display the result
print("the total terms is:", terms)
for i in range(terms):
   print("2 raised to power", i, "is", result[i])

输出:

('the total terms is:', 10)
('2 raised to power', 0, 'is', 1)
('2 raised to power', 1, 'is', 2)
('2 raised to power', 2, 'is', 4)
('2 raised to power', 3, 'is', 8)
('2 raised to power', 4, 'is', 16)
('2 raised to power', 5, 'is', 32)
('2 raised to power', 6, 'is', 64)
('2 raised to power', 7, 'is', 128)
('2 raised to power', 8, 'is', 256)
('2 raised to power', 9, 'is', 512)