Pow() function belongs to which library in python

In this tutorial, you will learn about the Python pow() method with the help of examples.

The pow() method computes the power of a number by raising the first argument to the second argument

Example

# compute 3^4 print(pow(3, 4));

# Output: 81


pow() Syntax

The syntax of pow() is:

pow(number, power, modulus [optional])

pow() Parameters

The pow() method takes three parameters:

  • number- the base value that is raised to a certain power
  • power - the exponent value that raises number
  • modulus - (optional) divides the result of number paused to a power and finds the remainder: number^power% modulus

pow() Return Value

The pow() method returns:

  • number^power - number, raised to a certain power
  • number^power % modulus - with the modulus argument
  • 1 - if the value of power is 0
  • 0 - if the value of number is 0

Example 1: Python pow()

# returns 2^2
print(pow(2, 2))  

# returns -2^2
print(pow(-2, 2))    

# returns 1/2^2 
print(pow(2, -2))   
 
# returns -1/-2^2 
print(pow(-2, -2))   

Output

4
4
0.25
0.25

The pow() method helps us find the value of a number raised to a certain power.

In the above example,

  • pow(2,2) is 22 - results in 4
  • pow(-2,2) is -22 - results in -4
  • pow(2,-2) is 1/22 - results in 0.25
  • pow(-2,-2) is -1/-22 - results in 0.25

Example 2: pow() with Modulus

x = 7
y = 2
z = 5

# compute x^y % z print(pow(x, y, z))

Output

4

In the above example, we have raised the number 7 to the power 2 which results in 49.

Since we have used the pow() method with three arguments x, y, and z, the third argument 5 divides the result of 72 and finds the remainder.

That's why we get the output 4.


Recommended Readings:

  • Python id()
  • Python int()

The pow() function computes the power of a number.

The pow() function takes two arguments (base value and power value) and, returns the power raised to the base number. For example,

[Mathematics] xy = pow(x, y) [In programming]

The pow() function is defined in math.h header file.


C pow() Prototype

double pow(double x, double y)

The first argument is a base value and second argument is a power raised to the base value.

To find the power of int or a float variable, you can explicitly convert the type to double using cast operator.

int base = 3;
int power = 5;
pow(double(base), double(power));

Example: C pow() function

#include <stdio.h>
#include <math.h>

int main()
{
    double base, power, result;

    printf("Enter the base number: ");
    scanf("%lf", &base);

    printf("Enter the power raised: ");
    scanf("%lf",&power);

    result = pow(base,power);

    printf("%.1lf^%.1lf = %.2lf", base, power, result);

    return 0;
}

Output

Enter the base number: 2.5
Enter the power raised: 3.4
2.5^3.4 = 22.54

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


Syntax

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




Description

The C library function double pow(double x, double y) returns x raised to the power of y i.e. xy.

Declaration

Following is the declaration for pow() function.

double pow(double x, double y)

Parameters

  • x − This is the floating point base value.

  • y − This is the floating point power value.

Return Value

This function returns the result of raising x to the power y.

Example

The following example shows the usage of pow() function.

#include <stdio.h>
#include <math.h>

int main () {
   printf("Value 8.0 ^ 3 = %lf\n", pow(8.0, 3));

   printf("Value 3.05 ^ 1.98 = %lf", pow(3.05, 1.98));
   
   return(0);
}

Let us compile and run the above program that will produce the following result −

Value 8.0 ^ 3 = 512.000000
Value 3.05 ^ 1.98 = 9.097324

math_h.htm

Which library has pow () function?

C library function - pow() The C library function double pow(double x, double y) returns x raised to the power of y i.e. xy.

What is pow () 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.

Is POW a built in function in Python?

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.

How do you find the POW in Python?

How to find the power of a number in Python.
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)).