Display Powers of 2 Using Normal Function in Python

(Last Updated On: 26/09/2023)

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

Python Program to Display Powers of 2 Using Normal Function

The first step in displaying the powers of 2 upto a certain number of terms is to define a function that will calculate the power of 2 for a given number. In this example, we have defined a function called “Func()” which takes one argument “x”, and returns 2 raised to the power of “x”.

Once we have the function, we can take a number as input from the user using the “input()” function and store 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.

After that, 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 “Func()” function that we have defined earlier, and pass the current value of “i” to it. This will calculate the power of 2 for each value of “i” in the range.

# Display Powers of 2 using Normal Function in python

# function receives a value as its argument & returns 2 raised to the power of the value.
def Func(x):
	return 2 ** x

# 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: "))

print()
for i in range(total_terms):
	print("2 raised to the power ", i, " is ", Func(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

One of the advantages of using a normal function in this example is that it makes the code more readable and easier to understand. The function “Func()” clearly states its purpose and can be reused in other parts of the program, which leads to better code organization and maintainability.

Another advantage of using function is that it makes the code more modular. This means that you can use the same function in different parts of the program with different arguments, and it will return the expected result. This makes the code more reusable and less prone to errors.

In conclusion, displaying the powers of 2 upto a certain number of terms in Python is a simple task that can be achieved using a normal function in combination with 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 normal function, input function, type conversion, and looping in Python.

With this tutorial, you now have a solid understanding of how to display the powers of 2 upto a certain number of terms using a normal function 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

About The Author

Leave a Reply