Python pow() Function with Examples

pow() Function in Python:

The pow() function returns the x raised to the power of y value (xy).

If there is a third parameter, it returns x to the power of y modulus z.

Syntax:

pow(x, y, z)

Parameters

x: It’s a number. It is the base value.

y: It’s a number. It is the exponent value.

z: This is optional. It is the modulus value.

Examples:

Example1:

Input:

Given base = 5
Given exponent = 2

Output:

The value of given base{ 5 } to the power{ 2 }=  25

Example2:

Input:

Given base = 5
Given exponent = 6
Given modulus = 7

Output:

The value of given base{ 5 } to the power{ 6 } modulus{ 7 } =  1

pow() Function with Examples in Python

Method #1: Using Built-in Functions (Static Input)

1) passing only two parameters

Approach:

  • Give the base value as static input and store it in a variable.
  • Give the exponent value as static input and store it in another variable.
  • Pass the given base and exponent values as the arguments to the pow() function to get the value of the given base raised to the power of the given exponent.
  • Store it in another variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Give the base value as static input and store it in a variable.
gvn_baseval = 5
# Give the exponent value as static input and store it in another variable.
gvn_expont = 2
# Pass the given base and exponent values as the arguments to the pow()
# function to get the value of the given base raised to the power of
# the given exponent.
# Store it in another variable.
rslt = pow(gvn_baseval, gvn_expont)
# Print the above result.
print("The value of given base{", gvn_baseval,
      "} to the power{", gvn_expont, "}= ", rslt)

Output:

The value of given base{ 5 } to the power{ 2 }=  25
2) passing three parameters

Approach:

  • Give the base value as static input and store it in a variable.
  • Give the exponent value as static input and store it in another variable.
  • Give the modulus value as static input and store it in another variable.
  • Pass the given base, exponent, and modulus values as the arguments to the pow() function to get the value of the given base raised to the power of the given exponent modulus given modulus value.
  • Store it in another variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Give the base value as static input and store it in a variable.
gvn_baseval = 6
# Give the exponent value as static input and store it in another variable.
gvn_expont = 2
# Give the modulus value as static input and store it in another variable.
gvn_moduls = 5
# Pass the given base, exponent, and modulus values as the arguments to the
# pow() function to get the value of the given base raised to the power
# of the given exponent modulus given modulus value.
# Store it in another variable.
rslt = pow(gvn_baseval, gvn_expont, gvn_moduls)
# Print the above result.
print("The value of given base{", gvn_baseval,
      "} to the power{", gvn_expont, "} modulus{", gvn_moduls, "} = ", rslt)

Output:

The value of given base{ 6 } to the power{ 2 } modulus{ 5 } =  1

Method #2: Using Built-in Functions (User Input)

1) passing only two parameters

Approach:

  • Give the base value as user input using the int(input()) function and store it in a variable.
  • Give the exponent value as user input using the int(input()) function and store it in another variable.
  • Pass the given base and exponent values as the arguments to the pow() function to get the value of the given base raised to the power of the given exponent.
  • Store it in another variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Give the base value as user input using the int(input()) function and store it in a variable.
gvn_baseval = int(input("Enter some random number = "))
# Give the exponent value as user input using the int(input()) function 
# and store it in another variable.
gvn_expont = int(input("Enter some random number = "))
# Pass the given base and exponent values as the arguments to the pow()
# function to get the value of the given base raised to the power of
# the given exponent.
# Store it in another variable.
rslt = pow(gvn_baseval, gvn_expont)
# Print the above result.
print("The value of given base{", gvn_baseval,
      "} to the power{", gvn_expont, "}= ", rslt)

Output:

Enter some random number = 2
Enter some random number = 5
The value of given base{ 2 } to the power{ 5 }= 32
2) passing three parameters

Approach:

  • Give the base value as user input using the int(input()) function and store it in a variable.
  • Give the exponent value as user input using the int(input()) function and store it in another variable.
  • Give the modulus value as user input using the int(input()) function and store it in another variable.
  • Pass the given base, exponent, and modulus values as the arguments to the pow() function to get the value of the given base raised to the power of the given exponent modulus given modulus value.
  • Store it in another variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Give the base value as user input using the int(input()) function and store it in a variable.
gvn_baseval = int(input("Enter some random number = "))
# Give the exponent value as user input using the int(input()) function 
# and store it in another variable.
gvn_expont = int(input("Enter some random number = "))
# Give the modulus value as user input using the int(input()) function 
# and store it in another variable.
gvn_moduls = int(input("Enter some random number = "))
# Pass the given base, exponent, and modulus values as the arguments to the
# pow() function to get the value of the given base raised to the power
# of the given exponent modulus given modulus value.
# Store it in another variable.
rslt = pow(gvn_baseval, gvn_expont, gvn_moduls)
# Print the above result.
print("The value of given base{", gvn_baseval,
      "} to the power{", gvn_expont, "} modulus{", gvn_moduls, "} = ", rslt)

Output:

Enter some random number = 5
Enter some random number = 6
Enter some random number = 7
The value of given base{ 5 } to the power{ 6 } modulus{ 7 } = 1