Python pow() Function

Python Built In Pow() Function

This is a detailed tutorial of the Python Built-in pow() Function. Learn to find the power of a given number or the modulus of the power of a given number.

Python pow() Function

This function returns the power of a given number or if wanted, also the modulus of the power of a given number. It is among one of the most used Python Functions for Mathematical Calculations.

Syntax

Given below are two different syntaxes for the function pow(). The first one calculates the x raised to the power of y while the second one calculates the modulus of z with the resultant value of x raised to the power of y.

pow() Parameters

This Python built-in function can take up to three arguments that are described below.

  • x. The base number in the term xy
  • y. The exponent number in the term xy
  • z. It is optional to be passed and is used to calculate the modulus of the xy and z.

pow() Return Value

The function pow() returns

  • the resultant value of xy if only x and y arguments are passed.
  • the resultant value of xy % z, if all the three arguments i.e. x, y and z are passed.

Examples

The first example illustrates the usage of the Python built-in function pow() with the first two arguments and then the second example illustrates the usage of this method with all of the three arguments.

Example 1. Calculating Power of a Number with pow()

Here we’re simply calculating 105, 10-5, -105, and -10-5.

x = 10
y = 5

#Calculating 10 raised to the power 5
print("10^5 :", pow(x, y))

#Calculating 10 raised to the power -5
print("10^-5 :", pow(x, -y))

#Calculating -10 raised to the power 5
print("-10^5 :", pow(-x, y))

#Calculating -10 raised to the power -5
print("-10^-5 :", pow(-x, -y))

Output.

Example 2. Calculating xy % z with using the pow() function

x = 3
y = 5
z = 2

#Calculating (3^5) % 2
print("(3^5) % 2 : ", pow(x, y, z))

The value of 3 raised to the power 5 is 243 and 243’s modulus with 2 gives 1. Therefore, the resultant value of pow(3, 5, 2) is 1 as shown in the following output.

Output.

I hope you found this guide useful. If so, do share it with others who are willing to learn Python and other programming languages. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *