** operator and the
pow() function can be used to find the power of a number in Python. The Python power function
** operator function the same way. The first number is raised to the power of the second,
4**5 would be 4 to the power of 5.
Calculating the power of a number is a common mathematical operation and one that Python supports in a number of ways.
For instance, if you’re creating a program that helps students in a sixth-grade math class revise powers, you would want to leverage the operation. Or if you are working with multiplication operations that multiply the same number many times over, you may want to use a power calculation.
In Python programming, the power of a number can be calculated in two ways: using the
** operator, and using the
pow() function. This tutorial will discuss how to use both of these techniques to calculate the power of a number and explore an example of each of these methods of calculating powers.
Python ** Operator
** operator is used to raise a number in Python to the power of an exponent. In other words,
** is the power operator in Python.
** operator requires two values to perform a calculation. Here’s the syntax for the
5 ** 2
In this example, the operators are evaluated, and the following result is returned:
In this expression, 5 is being raised to the 2nd power. Or, in other words, the number 5 is being multiplied by itself 3 times.
Let’s walk through an example to show how the
** operator can be used in Python. Let’s say we are creating an application that tests sixth grade math students on their knowledge of powers.
To do so, we want to present a student with a math problem, then we want to ask them for the answer. Our program will then calculate the answer to the problem and compare it to the answer the user has inserted into the program.
Here’s an example program which would allow us to test sixth-grade math students on their knowledge of powers:
number = 7 exponent = 2 student_answer = int(input("What is 7 to the power of 2?")) answer = 7 ** 2 if student_answer == answer: print("You're correct!") else: print("So close! 7 to the power of 2 is", answer)
When we execute our program and insert the answer 56, we get the following response:
What is 7 to the power of 2? 56 So close! 7 to the power of 2 is 49
As you can see, our program has calculated that our answer was incorrect, and returned a message with the correct answer.
Let’s break down our code. On the first line, we declare a variable called
number which stores the number we want to raise to a mathematical power. On the next line, we declare
exponent which is the exponent number to which we will raise the variable
On the next line, we use the
input() method to ask the user: What is 7 to the power of 2? We then use
int() data type conversion method to convert the user’s response to an integer so that we can work with the number in our code.
Then, we calculate the answer to the question using the
** operator. In this case, we use
7 ** 2 to calculate 7 to the power of 2.
Finally, we declare an
if statement that prints out the message
You’re correct! to the console if a user gets the right answer, and prints out a message with the right answer if a user is incorrect.
In addition, Python includes a built-in function that can be used to calculate powers:
pow() method calculates a certain number to the power of another number. First, the method converts its argument to a floating-point number. Then, it computes the power. The
pow() method will return the same value as using the
** operator because they both calculate the power of a number in the same way.
Here’s the syntax for the Python
pow(base, exponent, modulus)
pow() method accepts three parameters:
- base is the number which will be raised to the power of the exponent (required)
- exponent is the number to which the base will be raised (required)
- modulus executes a modulo calculation on the result of base
**exponent. If modulus is specified, the base and exponent must be integers, and the exponent must be a positive value. (optional)
If the first two arguments are specified, the stated base to the power of the exponent is calculated. If the third argument is also specified, the stated base to the power of exponent is calculated, then the modulo of the modulus is returned. In other words, the
pow() method will execute: pow(base, exponent) % modulus. This is a more advanced function with specific use cases, so we’ll not discuss it in detail in this article.
Let’s use our example from above to explain how the
pow() method works. Say that we are creating a game to test sixth-graders on their knowledge powers in math. Our program asks a user for an answer to a question, calculates the answer, then compares whether the user’s answer is the same as the one computed by the program.
Instead of using the
** operator, we could use
pow() to calculate the power of the numbers in our code. Here’s an example of
pow() being used with the code we used for our sixth-grade power game:
number = 7 exponent = 2 student_answer = int(input("What is 7 to the power of 2?")) answer = pow(7, 2) if student_answer == answer: print("You're correct!") else: print("So close! 7 to the power of 2 is", answer)
Our code is identical to the first example with one difference. Instead of declaring
answer = 7 ** 2, we assign the
answer variable the value
pow(7, 2). If we insert the answer 49 into our code, the following response is returned:
What is 7 to the power of 2? 49 You're correct!
As you can see, our code works in the same way as the first example, but we use the
pow() method to calculate the power of our numbers instead of the
** operator is used to raise a number to the power of an exponent. In addition, the
pow() method is used for the same purpose.
This tutorial discussed how to use both the
** operator and
pow() to calculate powers in Python. We also covered an example of each method of calculating powers. Now you’re equipped with the knowledge required to calculate powers in Python like a pro!