Sunday, November 17, 2024
Google search engine
HomeLanguagesPython pow() Function

Python pow() Function

Python pow() function returns the result of the first parameter raised to the power of the second parameter.

Syntax of pow() Function in Python

Syntax: pow(x, y, mod)

Parameters : 

  • x : Number whose power has to be calculated.
  • y : Value raised to compute power.
  • mod [optional]: if provided, performs modulus of mod on the result of x**y (i.e.: x**y % mod)

Return Value :  Returns the value x**y in float or int (depending upon input operands or 2nd argument).

Python pow() Function Example

The pow() in Python is simple to use, just pass two values as the parameters.

Python3




print(pow(3,2))


Output:

9

Power Function in Python with Modulus 

We can pass more than two parameters to the pow() in Python. The first parameter is the number on which we want to apply the Pow() function, the second parameter is the power and the third parameter is to perform the modulus operation.

It return the value of 3 to the power of 4, modulus 10 (same as (3 * 3 * 3 * 3) % 10)

Python3




# Python code to demonstrate pow()
print("The value of (3**4) % 10 is : ", end="")
 
# Returns 81%10
# Returns 1
print(pow(3, 4, 10))


Output: 

The value of (3**4) % 10 is : 1

Implementation Cases in pow() Function in Python 

The below table summarises the different cases to apply the Python pow() function.

Cases of  Python pow() function

Cases of  Python pow() function

Example:

Python code to discuss negative and non-negative cases.

 

Python3




# positive x, positive y (x**y)
print("Positive x and positive y : ", end="")
print(pow(4, 3))
 
print("Negative x and positive y : ", end="")
# negative x, positive y (-x**y)
print(pow(-4, 3))
 
print("Positive x and negative y : ", end="")
# positive x, negative y (x**-y)
print(pow(4, -3))
 
print("Negative x and negative y : ", end="")
# negative x, negative y (-x**-y)
print(pow(-4, -3))


Output:

Positive x and positive y : 64
Negative x and positive y : -64
Positive x and negative y : 0.015625
Negative x and negative y : -0.015625

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments