In this example, you will learn to compute the power of a number.
To understand this example, you should have the knowledge of the following Python programming topics:
- Python pow[]
- Python for Loop
- Python while Loop
Example 1: Calculate power of a number using a while loop
base = 3
exponent = 4
result = 1
while exponent != 0:
result *= base
exponent-=1
print["Answer = " + str[result]]
Output
Answer = 81
In this program, base
and exponent
are assigned values 3 and 4 respectively.
Using the while loop, we keep on multiplying the result
by base
until the exponent
becomes zero.
In this case, we multiply result
by base 4 times in total, so result = 1 * 3 * 3 * 3 * 3 = 81
.
Example 2: Calculate power of a number using a for loop
base = 3
exponent = 4
result = 1
for exponent in range[exponent, 0, -1]:
result *= base
print["Answer = " + str[result]]
Output
Answer = 81
Here, instead of using a while loop, we've used a for loop.
After each iteration, the exponent is decremented by 1, and the result is multiplied by the base exponent number of times.
Both programs above do not work if you have a negative exponent. For that,
you need to use the pow[]
function in the Python library.
Example 3: Calculate the power of a number using pow[] function
base = 3
exponent = -4
result = pow[base, exponent]
print["Answer = " + str[result]]
Output
Answer = 0.012345679012345678
pow[]
accepts two arguments: base and exponent. In the above example, 3 raised to the power -4 is calculated using pow[]
.
How to use the pow[]
function from the math
module
Import the math module as shown below in your Python environment.
Use the pre-defined
math.pow[number,exponent]
function to find the power of the number.
import math print[math.pow[4,2]]
Importing math module in Python
How to find the power through iterationfor…loop
Define the function, which takes two arguments:
- number
- exponent/power
Find the power of a number: multiply the number with itself the same number of times as the exponent’s value. We use the for…loop to achieve this task and store the result in the res variable.
Return the result stored in res, which is the required power of a number.
Finally, display the output.
def power[n,e]: res=0 for i in range[e]: res *= n return res print[pow[4,2]]
Finding Power of a Number using Iteration.
How to find the power through recursive function
Define the function, which takes two arguments:
- number
- exponent/power
If the exponent/power is 0, then return 1.
If the exponent/power is 1 then return n, i.e., the number itself.
Otherwise, multiply the current number and call the
power[n,e]
function recursively, with e [exponent/power] decremented by 1n*power[n, e-1] each time the function is called.Finally, display the output, which is the desired power of the number.
def power[n, e]: if e == 0: return 1 elif e == 1: return n else: return [n*power[n, e-1]] n = 4 p = 2 print[power[n, p]]
Recursive function to find power of a number.
RELATED TAGS
python
program
math
communitycreator
CONTRIBUTOR
❮ Built-in Functions
Example
Return the value of 4 to the power of 3 [same as 4 * 4 * 4]:
x = pow[4, 3]
Try it Yourself »
Definition and Usage
The pow[]
function returns the value of x to the power of y [xy].
If a third parameter is present, it returns x to the power of y, modulus z.
Syntax
Parameter Values
x | A number, the base |
y | A number, the exponent |
z | Optional. A number, the modulus |
More Examples
Example
Return the value of 4 to the power of 3, modulus 5 [same as [4 * 4 * 4] % 5]:
x = pow[4, 3, 5]
Try it Yourself »
❮ Built-in Functions