# How do you write a power function in python?

❮ 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.

## Parameter Values

ParameterDescription
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

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.

• Career Karma matches you with top tech bootcamps
• Access exclusive scholarships and prep courses

First name

Last name

Email

Phone number

## Python Power

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:

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?"))

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.

### 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.

"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"

Venus, Software Engineer at Rockbot

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?"))

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!```

## Conclusion

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.

### How do you make a power function in Python?

pow(number,exponent) function to find the power of the number..
import math. print(math. pow(4,2)) Run. Importing math module in Python..
def power(n,e): res=0. for i in range(e): res *= n. return res. print(pow(4,2)) Run. ... .
def power(n, e): if e == 0: return 1. elif e == 1: return n. else: return (n*power(n, e-1)).

### Is there a power function in Python?

Python pow() Function 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.

### How do you enter a power in Python?

Power (exponent) operator The operator that can be used to perform the exponent arithmetic in Python is ** . Given two real number operands, one on each side of the operator, it performs the exponential calculation ( 2**5 translates to 2*2*2*2*2 ).

### What is pow () function?

pow() The Math.pow() method returns the value of a base raised to a power. That is. 𝙼𝚊𝚝𝚑.𝚙𝚘𝚠 ( 𝚡 , 𝚢 ) = x y.

Tải thêm tài liệu liên quan đến bài viết How do you write a power function in python?