Program To Display Powers of 2 till N

Python Program to Display Powers of 2 till N

Given the Number N , the task is to print the powers of 2 till N.

Examples:

Example1:

Input:

Number = 10

Output:

The total terms of the number = 10
Value of 2 power 0 = 1
Value of 2 power 1 = 2
Value of 2 power 2 = 4
Value of 2 power 3 = 8
Value of 2 power 4 = 16
Value of 2 power 5 = 32
Value of 2 power 6 = 64
Value of 2 power 7 = 128
Value of 2 power 8 = 256
Value of 2 power 9 = 512

Example2:

Input:

Number = 20

Output:

The total terms of the number = 20
Value of 2 power 0 = 1
Value of 2 power 1 = 2
Value of 2 power 2 = 4
Value of 2 power 3 = 8
Value of 2 power 4 = 16
Value of 2 power 5 = 32
Value of 2 power 6 = 64
Value of 2 power 7 = 128
Value of 2 power 8 = 256
Value of 2 power 9 = 512
Value of 2 power 10 = 1024
Value of 2 power 11 = 2048
Value of 2 power 12 = 4096
Value of 2 power 13 = 8192
Value of 2 power 14 = 16384
Value of 2 power 15 = 32768
Value of 2 power 16 = 65536
Value of 2 power 17 = 131072
Value of 2 power 18 = 262144
Value of 2 power 19 = 524288

Program To Display Powers of 2 till N in Python

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.

Method #1:Using ** operator

We can calculate the power of the 2 of the given number using ** operator.

We can take a loop from 0 to number and print the power of 2 of the given iterator value.

Print the power of 2 of given iterator value.

Below is the implementation:

# given number n
number = 10
# printing the power of 2 of the iterator value
for i in range(number):
    # calculating the power_of_2 of i
    powvalue = 2**i
    print("Value of 2 power", i, "=", powvalue)

Output:

Value of 2 power 0 = 1
Value of 2 power 1 = 2
Value of 2 power 2 = 4
Value of 2 power 3 = 8
Value of 2 power 4 = 16
Value of 2 power 5 = 32
Value of 2 power 6 = 64
Value of 2 power 7 = 128
Value of 2 power 8 = 256
Value of 2 power 9 = 512

Method #2:Using Anonymous function in Python

To determine the powers of 2 in the program below, we utilized the anonymous (lambda) function inside the map() built-in function. In Python, an anonymous function is one that is not given a name.
The def keyword is used to define conventional functions, whereas the lambda keyword is used to define anonymous functions in Python. As a result, anonymous functions are referred to as lambda functions.

Syntax:

lambda arguments: expression

Lambda functions can take any number of parameters but can only execute one expression.The result is returned once the expression has been evaluated.

Below is the implementation:

# Using the anonymous function, display the powers of two.
# given number n
number = 10
# use anonymous function to print powers of 2 till given number
resultTerms = list(map(lambda x: 2 ** x, range(number)))
print("The total terms of the number = ", number)
# print the powers of 2 till number
for i in range(number):
    print("Value of 2 power", i, "=", resultTerms[i])

Output:

The total terms of the number = 10
Value of 2 power 0 = 1
Value of 2 power 1 = 2
Value of 2 power 2 = 4
Value of 2 power 3 = 8
Value of 2 power 4 = 16
Value of 2 power 5 = 32
Value of 2 power 6 = 64
Value of 2 power 7 = 128
Value of 2 power 8 = 256
Value of 2 power 9 = 512

Explanation:

The following statement appears in the above program:

result = lambda x: 2 ** x specifies that any value we pass to result later is transferred to x, and 2 ** x is returned. result (3) returns 2 ** 3 or 2*2*2 or 8 in this case.
To make a list, use the list() function. After applying the function to each item of a provided iterable (in the example of the previous application, a list), map() produces an iterator of results.

 
Related Programs: