Question:Display powers of two using anonymous function

**Program:**

1 2 3 4 5 6 7 8 9 10 11 12 | # Python Program to display the powers of 2 using anonymous function 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]) |

**Explanation:**

The lambda operator or lambda function is a way to create small anonymous functions, i.e. functions without a name. These functions are throw-away functions, i.e. they are just needed where they have been created.

The general syntax of a lambda function is quite simple:

lambda argument_list: expression

The argument list consists of a comma separated list of arguments and the expression is an arithmetic expression using these arguments. You can assign the function to a variable to give it a name.

**Output:**

1 2 3 4 5 6 7 | How many terms? 5 ('The total terms is:', 5) ('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) |

Do share your feedback in comments to help us adding more programs like this.

## Leave a Reply