Table of Contents
- Python power – for loop
- Python power – ** operator
- Python power – pow() function
Whether you are learning Python or are already an expert, you will surely have had to calculate the power of a number.
In this article we will see what options Python gives us to do this task.
We will do this as usual with clear explanations and full of examples so that there are no doubts as to how to solve the problem you are facing.
Python power – for loop
The first approach I want to show you uses a for loop.
We will therefore create our own function that we can then recall when needed.
Python offers other methods that are certainly faster but this remains the most didactic solution and is worth seeing.
We then proceed to create our own function which we will call
def power_function(base, exponent): # initialize to 1 the result to cover the case where the exponent is 0 result = 1 # use a for loop to do the power for i in range(exponent): result *= base # return the result return result
As you can see from the code above, the first thing our function does is to initialize the final result to 1.
This is done to cover the case where the exponent is 0.
Subsequently, if the exponent is non-zero, the base will be multiplied by itself a number of times equal to the exponent.
At the end of this operation the final result will be returned.
Let’s now see some uses of this function:
print(power_function(2, 3)) print(power_function(5, 2)) print(power_function(2, 0)) print(power_function(10, 2))
8 25 1 100
Python power – ** operator
Another option that Python gives us to calculate the power of a number is to use the ** operator.
This operator is really simple to use, let’s see the syntax:
power = base**exponent
Now let’s try the same examples of the previous section and we will see that the results will be the same!
print(2**3) print(5**2) print(2**0) print(10**2)
8 25 1 100
Python power – pow() function
Last but not least, Python provides a built-in function called pow(), here is the syntax.
pow(base, exponent[, modulo])
The Python pow() function returns the elevated base to the exponent.
Let’s leave out the optional
modulo parameter for a moment and see some examples of how this works.
print(pow(2, 3)) print(pow(5, 2)) print(pow(2, 0)) print(pow(10, 2))
8 25 1 100
As you can see the pow() function behaves exactly like the methods shown in the previous sections.
There is also an added option, namely the modulo.
We explain this parameter with an example:
pow(2, 3, 2)
In this case the operations carried out are the following:
- 2 * 2 * 2 = 8
- 8 % 2, ie we take the remainder of the division
Here are some additional examples to better understand the concept:
pow(2, 3, 2) pow(6, 2, 5) pow(4, 3, 4)
0 1 0
This post showed you several methods to raise a number to the power in Python.
I hope this post was useful to you and I invite you to ask in the comments below if you have any doubts or if you are in trouble.
If this topic is clear to you, take a look at the latest posts!