Python pow()

The pow() method returns x to the power of y. If the third argument (z) is given, it returns x to the power of y modulus z, i.e. pow(x, y) % z.

The pow() method returns x to the power of y. If the third argument (z) is given, it returns x to the power of y modulus z, i.e. pow(x, y) % z.

The syntax of pow() method is:

pow(x, y[, z])

The pow(x, y) is equivalent to:

x**y

pow() Parameters

The pow() method takes three parameters:

  • x - number which is to be powered
  • y - number which is to be powered with x
  • z (Optional) - number which is to be used for modulus operation

pow() method parameters cases

x

y

z

Non-negative Integer OR Negative Integer

Non-negative Integer

May or may not be present

Non-negative Integer OR Negative Integer

Negative Integer

Should not be present

Return value from pow()

The pow() method's return value depends upon the type of arguments passed.

pow() method return value cases

x

y

z

Return Value

Non-negative Integer

Non-negative Integer

N/A

Integer

Non-negative Integer

Negative Integer

N/A

Float

Negative Integer

Non-negative Integer

N/A

Integer

Negative Integer

Negative Integer

N/A

Integer

Non-negative Integer OR Negative integer

Non-negative Integer

Non-negative Integer OR Positive Integer

Integer

Example 1: How pow() works in Python?

# positive x, positive y (x**y)

print(pow(2, 2))

# negative x, positive y

print(pow(-2, 2))

# positive x, negative y (x**-y)

print(pow(2, -2))

# negative x, negative y

print(pow(-2, -2))

When you run the program, the output will be:

4

4

0.25

0.25

Example 2: pow() with three arguments (x**y) % z

x = 7

y = 2

z = 5

print(pow(x, y, z))

When you run the program, the output will be:

4

Here, 7 is powered by 2 (7**2) which equals 49. Then, 49 modulus 5 (49 % 5) equals 4.

Add comment

CommentList

The file '/Custom/Widgets/ScriptInjection/widget.cshtml' does not exist.