codescracker


python

Python Program to Display Powers of 2



« Previous Program Next Program »


Display Powers of 2 in Python

To display powers of 2 in python, you have to ask from user to enter upto how many terms he/she want to print the powers of two, and then print powers of 2 upto the given term as output as shown in the program given here.

Python Programming Code to Display Powers of 2

Following python program ask from user to enter limit to display powers of 2 upto the given limit:

# Python Program - Display Powers of 2

print("Enter 'x' for exit.");
limit = input("Upto how many terms to print power of 2? ")
if limit == 'x':
    exit();
else:
    term = int(limit);
    print();
    res = list(map(lambda x: 2 ** x, range(term)));
    for i in range(term):
        print("2 raised to the power",i,"=",res[i]);

Here is the sample run of the above Python program to illustrate how to find and print powers of 2:

print power of 2 python

Above is the initial output asking to enter total number of term or limitation upto which you want to print power of two, for example, if you will provide the limitation or input as 10, then here is the output you will see:

2 raised to the power 0 = 1
2 raised to the power 1 = 2
2 raised to the power 1 = 4
2 raised to the power 1 = 8
2 raised to the power 1 = 16
2 raised to the power 1 = 32
2 raised to the power 1 = 64
2 raised to the power 1 = 128
2 raised to the power 1 = 256
2 raised to the power 1 = 512

Now let's check it out by supplying 10 as input and press enter key, then see the output given below:

powers of 2 python

Here is the same program with run on python shell:

python display powers of 2

Program Video with Compile and Run


« Previous Program Next Program »








Tools
Calculator

Quick Links
Signup - Login - Give Online Test