The ** operator and the pow() function calculate the power of a number in Python. The ** operator raises the number on the left to the power of the number on the right. The pow() function raises the first parameter to the power of the second parameter.
Calculating the power of a number is a common mathematical operation. For instance, if you’re creating a program that helps students in a sixth-grade math class revise powers, you would need a power function.
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. We’ll walk through an example of how to use both the ** operator and the pow() method.
Python Power: ** Operator
The Python ** operator is used to raise a number in Python to the power of an exponent. In other words, ** is the power operator in Python.
The ** operator requires two values to perform a calculation. Here’s the syntax for the ** operator:
5 ** 2
Our program returns the following result: 25.
In this expression, 5 is raised 2nd power. Or, in other words, the number 5 is multiplied by itself 3 times.
Python ** Operator Example
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.
On the first line, we declare a variable called number which stores the number we want to raise to a mathematical power. Next, we declare exponent which is the exponent number to which we will raise the variable number.
On the next line, we use the Python input() method to ask the user: What is 7 to the power of 2?
We use Python int() data type conversion method to convert the user’s response to an integer. This is necessary because input() returns a string, and we can only use the ** operator with numbers.
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.
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.
Power Python: pow() Method
Python includes a built-in function that can be used to calculate powers: pow(). pow() accepts three parameters: a base number, an exponent to which the base is raised, and a modulo operator.
The 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.
Here’s the syntax for the Python pow() method:
pow(base, exponent, modulus)
The pow() method accepts three parameters:
- base is the number raised to the power of the exponent (required)
- exponent is the number to which the base is 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, pow() returns the modulus of the calculated number. This is a more advanced function with specific use cases, so we’ll not discuss it in detail in this article.
To learn more about the Python modulo operator, read our Python modulo operator guide.
Python pow() Method Example
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 and calculates the answer. Then, our code 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 similar 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 program returns:
What is 7 to the power of 2? 49 You're correct!
The ** operator and pow() method raise a number to the power of another number. These are the Python power methods.
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!
If you’re looking for more learning resources to help you master Python, check out our How to Learn Python guide.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Read more