Exponentiation in Python | Power Operator

Python provides multiple operators that are used to perform various tasks, i.e., Arithmetic, Assignment, Comparison, Logical, etc. Power Operator belongs to the Arithmetic operators category of Python. It is used to perform the exponent calculation on the given operand number, i.e., base and exponent.

This Python write-up provides a detailed analysis of exponentiation in Python with multiple examples. The following points are discussed in this Python article:

So, let’s begin!

What is Exponentiation in Python?

Exponentiation is a mathematical operation with two numbers, an exponent and a base. The base number is multiplied by itself, and the multiplication count depends on the number in the exponent. The exponent operator “**” or the pow() function is used in Python to ease this process. The working of the exponent operator is just like the “pow()” and the “math.pow()” functions.

Syntax

Follow one of the below-given syntaxes to perform the exponentiation in Python:

x**y
or
x^y

In the above snippet, the “x” represents the base number and the “y” represents the exponent number.

Example 1: Using Exponent Operator

The exponent operator ** is used to find the power of a number or perform exponent arithmetic calculations in Python. Let’s see an example code to know how to use the exponent operator in Python:

Code:

Num_1 = 2
Num_2 = 5
Output = Num_1 ** Num_2
print(Output)

In the above code:

  • The integer values are initialized to the variables named “Num_1” and “Num_2”.
  • The exponent operator “**” is used to find the power of the first variable raised to the power of the second variable like “2^5”.

Output:

This way, you can perform the exponentiation in a Python program using the exponent operator.

Example 2: Using pow() Function

The inbuilt “pow()” function is utilized in the Python program to find the power of the first(base) number raised to the power of the second(exponent) number. Below is an example of how the “pow()” function works:

Code:

Num_1 = 2
Num_2 = 5
Output = pow(Num_1, Num_2)
print(Output)

In the above code:

  • The “pow()” function takes the base number as a first parameter and exponent number as a second parameter to find the power of a value.
  • Here, the variable “Num_1” is a base number, and “Num_2” is an exponent number.
  • The “pow()” function The “pow()” function will retrieve 2^5.

Note: The pow() function will throw an error if the given base value is negative and the power/exponent value is not an integer.

Output:

The power of the base number according to the exponent is calculated in the above output.

Example 3: Using math.pow() Function

The “math.pow()” function of the “math” module is utilized to find the power of the base number according to the exponent number. The given below code shows the concept of the “math.pow()” function:

Code:

import math

Num_1 = 2
Num_2 = 5.5
Output = math.pow(Num_1, Num_2)
print(Output)

In the above code snippet:

  • To access the “math.pow()” function, the “math” module is imported at the beginning of the program.
  • The “math.pow()” function accepts a base number and an exponent number as arguments and returns the base number power.
  • The “math.pow()” function always returns numbers in float data type, unlike the simple “pow()” function.

Output:

The above output calculated the power of the input number successfully.

That’s it, folks.

Conclusion

The exponent or power operator calculates the exponent of the given numbers. Exponent operators perform calculations on two values, i.e., base and exponent. The “pow()” and the “math.pow()” functions are also used to find the power of an input number in Python, just like an exponent operator. This article presented a detailed analysis of Python exponentiation operators with numerous examples.