Python program to find power of a number
Last Updated :
21 Feb, 2025
The task of finding the power of a number in Python involves calculating the result of raising a base number to an exponent. For example, if we have a base 2 and an exponent 3, the result is 2^3=8 .
Using ** operator
This is the simplest and most Pythonic way to calculate the power of a number. The ** operator is internally optimized and is the preferred choice for most situations.
Python
N, X = 2, 3
res = N ** X
print(res)
Explanation: The expression res = N ** X is used to calculate the power of N raised to X using Python's exponentiation operator ** . This evaluates to 2^3=8 .
Using pow()
pow() function is another efficient built-in method. It performs the same task as **, with the added advantage of supporting modular arithmetic for advanced use cases.
Python
N, X = 2, 3
res = pow(N, X)
print(res)
Explanation: pow() function in res = pow(N, X) computes 2^3 =8, raising N to the power X.
Using recursion
This approach uses recursion to divide the exponent into halves and reduce the number of operations. It is an elegant solution but involves a function call stack, which increases space complexity.
Python
def power(N, X):
if X == 0:
return 1 # base case
half = power(N, X // 2) # recursive call to get half power
if X % 2 == 0:
return half * half # if even, multiply half powers
else:
return half * half * N # if odd, multiply extra N
N, X = 2, 3 # initialize base (N) and exponent (X)
print(power(N, X))
Explanation: This program defines a recursive function power(N, X) to calculate N^X . It returns 1 if X is 0. It divides the problem into halves using power(N, X // 2), multiplying results accordingly. If X is even, it returns half * half; if odd, half * half * N.
Using loop
This is the most straightforward method, using a loop to multiply the base N repeatedly X times. While easy to understand, it is inefficient for large exponents.
Python
N, X = 2, 3
P = 1 # initialize result
for i in range(X): # loop X times
P = P * N
print(P)
Explanation: for loop runs X times (3 times), and in each iteration, P is multiplied by N. This results in 2×2×2=8.
Similar Reads
Python Program to Find Cube of a Number We are given a number and our task is to find the cube of this number in Python. The cube of a number is calculated by multiplying the number by itself twice. For example, if the input is 3, then the output will be 3 * 3 * 3 = 27. In this article, we will learn different ways to find the cube of a n
2 min read
Python Program to Check Number is a Power of Two we will discuss how to write a Python program to determine whether a given number is a power of two. A number is said to be a power of two if it can be expressed in the form of 2n, where n is a non-negative integer.Examples: Pythondef is_power_of_two(n): if n <= 0: return False return (n & (n
4 min read
Python program to find the power of a number using recursion Given a number N and power P, the task is to find the power of a number ( i.e. NP ) using recursion. Examples: Input: N = 2 , P = 3Output: 8 Input: N = 5 , P = 2Output: 25 Approach: Below is the idea to solve the above problem: The idea is to calculate power of a number 'N' is to multiply that numbe
3 min read
Python Program to Get K initial powers of N Given size K and value N, the task is to write a Python Program to compute a list of powers of N till K. Input : N = 4, K = 6 Output : [1, 4, 16, 64, 256, 1024] Explanation : 4^i is output till i = K. Input : N = 3, K = 6 Output : [1, 3, 9, 27, 81, 243] Explanation : 3^i is output till i = K. Method
6 min read
Python program to calculate square of a given number The task of calculating the square of a number in Python involves determining the result of multiplying a number by itself. For example, given the number 4, its square is 16 because 4 Ã 4 = 16.Using ** operatorexponentiation operator (**) is the most direct and optimized way to compute powers. Since
1 min read