Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python pow() function

Python pow() function

Python Numbers Python Numbers


Description

pow() The method returns the value of x to the power of y (x^y).


Syntax

The syntax of the math module's pow() method is as follows:



import math



math.pow( x, y )

The built-in pow() method



pow(x, y[, z])

The function computes x raised to the power of y; if z is present, the result is then taken modulo z, which is equivalent to pow(x,y) %z.

**Note:** pow() is called directly as a built-in method; the built-in method treats arguments as integers, while the math module converts them to float.


Parameters


Return Value

Returns x

y

(x raised to the power y).


Example

The following shows examples of using the pow() method:

Example

#!/usr/bin/python

-- coding: UTF-8 --

import

math

Import the math module

print

"

math.pow(100, 2) :

"

,

math

.

pow

(

100

,

2

)

Use the built-in version and see the difference in output

print

"

pow(100, 2) :

"

,

pow

(

100

,

2

)

print

"

math.pow(100, -2) :

"

,

math

.

pow

(

100

, -

2

)

print

"

math.pow(2, 4) :

"

,

math

.

pow

(

2

,

4

)

print

"

math.pow(3, 0) :

"

,

math

.

pow

(

3

,

0

)

Running the example above produces:



math.pow(100, 2) :  10000.0

pow(100, 2) :  10000

math.pow(100, -2) :  0.0001

math.pow(2, 4) :  16.0

math.pow(3, 0) :  1.0

Python Numbers Python Numbers