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:
- What is Exponentiation in Python?
- Example 1: Using Exponent Operator
- Example 2: Using pow() Function
- Example 3: Using math.pow() Function
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.