# Display Powers of 2 upto N Numbers in Python

(Last Updated On: 26/09/2023)

Welcome to our tutorial on how to display the powers of 2 upto a certain number of terms in Python! In this tutorial, we will be discussing how to use the built-in “range()” function and the “**” operator to display the powers of 2 upto a certain number of terms.

## Python Program to Display Powers of 2 upto N Numbers

The first step in displaying the powers of 2 upto a certain number of terms is to take a number as input from the user. In this example, we are using the “input()” function to take a number from the user and storing it in the variable “total_terms”. It’s worth noting that, the input() function returns the value as a string, so we will have to convert the string to an int before using it.

Once we have the number of terms, we can use the built-in “range()” function to loop through a range of numbers up to the number of terms. The “range()” function takes one or more arguments, and returns an iterator that generates a sequence of numbers.

Inside the loop, we can use the “” operator to calculate the power of 2 for each number in the range, and store it in a variable called “result”. The “” operator is used for exponentiation in Python. It raises the number on the left of the operator to the power of the number on the right of the operator.

```# Display Powers of 2 Upto N numbers in python

# we are taking a number from user as input
# entered value will be converted to int from string
total_terms = int(input("Enter the Total Number of Terms: "))

for i in range(total_terms):
print("2 raised to the power ", i, " is ", 2 ** i)
```

#### Output:

Finally, we can use the “print()” function to display the result of the calculation.

``````Enter the Total Number of Terms: 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
``````

In conclusion, displaying the powers of 2 upto a certain number of terms in Python is a simple task that can be achieved using the built-in “range()” function and the “**” operator. By understanding how to use these functions and operators, you’ll be able to control the number of terms and the layout of your output in a more precise way.

Additionally, this example illustrates the use of input functions, type conversion, and looping in Python. With this tutorial, you now have a solid understanding of how to display the powers of 2 upto n numbers in Python and how to use these techniques in your own projects.

For More Python Programming Exercises and Solutions check out our Python Exercises and Solutions