# Python Program To Display Powers of 2 Using Anonymous Function (lambda Function)

By | October 3, 2021 Here in this article, we have provided a python program that can print the powers of 2 using the python lambda or Anonymous function.

## Prerequisite topics to create this Program

• Python Input, Output
• Python Lambda function.
• Python map function
• Python Operators
• Python Loop

#### Steps

• First, we ask the user to enter a number n, which represents up to which the user wants to print the power of 2.
• Using the int() function we will convert the entered string into an integer value.
• Using the map and lambda functions we will create a map object which contains all the values of 2 raised to the power n.
• Using a list we will convert the map object into a list
• At last using, for loop, we will print all the 2 raised to the power n values.

Note:

map(function, container): map() is an in-built and powerful function in python which accepts 2 arguments function and container (list, tuple, etc..). The map function picks a value from the container one by one and passes it into the function for each value.

lambda function: It is also known as Anonyms function and it is an easy and quick hand technique to write small user-defined functions.

#### Python Program To Display Powers of 2 Using Anonymous Function (lambda Function):

Code:

```n = int(input("Enter the number up to which you want to print the power of 2:" ))

pow_2_lambda = lambda x:2**x
list_of_2_pow = list(map(pow_2_lambda,list(range(n+1))))
for i in range(n+1):
print("2 raised to the power", i, "is", list_of_2_pow[I])```

Output 1:

```Enter the number up to which you want to print the power of 2:10
2 raised to the power 0 is 1
2 raised to the power 1 is 2
2 raised to the power 2 is 4
2 raised to the power 3 is 8
2 raised to the power 4 is 16
2 raised to the power 5 is 32
2 raised to the power 6 is 64
2 raised to the power 7 is 128
2 raised to the power 8 is 256
2 raised to the power 9 is 512
2 raised to the power 10 is 1024```

Output 2:

```Enter the number up to which you want to print the power of 2:17
2 raised to the power 0 is 1
2 raised to the power 1 is 2
2 raised to the power 2 is 4
2 raised to the power 3 is 8
2 raised to the power 4 is 16
2 raised to the power 5 is 32
2 raised to the power 6 is 64
2 raised to the power 7 is 128
2 raised to the power 8 is 256
2 raised to the power 9 is 512
2 raised to the power 10 is 1024
2 raised to the power 11 is 2048
2 raised to the power 12 is 4096
2 raised to the power 13 is 8192
2 raised to the power 14 is 16384
2 raised to the power 15 is 32768
2 raised to the power 16 is 65536
2 raised to the power 17 is 131072```