# Python Program to Display Powers of 2 Using Anonymous Function

Python power of 2 in mathematics mean that a number that is obtained after multiplying 2 by itself. For example, when the power of two is two, it means that 2 is multiplied by 2, thus providing the answer as 4. When the power of 2 is 3, then that means the multiplication occurs thrice, that is, 2 x 2 x 2, that is equal to 8, and so on.

To find whether a number can be written in Python powers of 2, the code will need an anonymous function. It is a function that is defined without specifying or requiring its name is called an anonymous function. While the normal functions are defined using the def keyword, for the anonymous function, the lambda keyword is used.

## Source Code

To understand the program for Python power of 2, one must understand the usage of loops in Python. Also, the anonymous, or lambda function used in the program is used inside the built-in function called map ().

                    
num = int (input (“Enter the number of terms: “))

result = list (map (lambda x: 2 ** x, range (num)))

print (“The total number of terms are:”, num)

for i in range (num):

print (“2 raised to the power”, i, “is”, result [i])



Here, the program first asks the user to input the number of terms until which they want the power of 2 to be calculated. Then, it saves the powers in the variable named result. The powers of 2 are calculated with the lambda function.

Next, all the numbers that are found by raising the power of 2 from 0 to the number of terms entered are printed with the use of FOR loop. Note that the operator that is used to calculate the power is ** in Python. Thus, 2 ** 2 will give 4, 2 ** 3 will give 8, and so on.

You can also use the normal function to calculate the powers of two as well. The function will keep calculating and printing the power of 2 as per the instructions provided by the user.

## Questions

How do you check if a number is a power of 2 in Python?

To check if a number is a power of two, it will need to be factored. If the number has any other prime factor other than 2, then it cannot be written in the powers of 2.

What are the powers of 2?

Powers of 2 are the numbers that are obtained when 2 is raised to powers of any natural numbers. For example, 2 to the power 4 is 2 x 2 x 2 x 2, that is, 16.

How do you find what the power of 2 a number is?

To find the power of 2, it will need to be multiplied by itself as many times as the power itself. Thus, if the power of 2 is three, it is multiplied by itself thrice.

How do you calculate power in Python?

To calculate the power of any number in Python, the ** operator is used. The first number that is placed on the left of the operator is the base and the second number that is placed at the right of the operator is the power.

Share with friends

## Customize your course in 30 seconds

##### Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
Ashhar Firdausi
IIT Roorkee
Biology
Dr. Nazma Shaik
VTU
Chemistry
Gaurav Tiwari
APJAKTU
Physics
Get Started