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.