Python math.exp2() Method
The Python math.exp2() method is used to calculate 2 raised to the power of a given number i.e 2x. It calculates the exponential method with base 2. Mathematically, the method is represented as −
exp2(x) = 2x
For example, if x = 3, then the exponential method exp2(3) evaluates to 23, which is equal to 8. In other words, when you multiply 2 by itself three times, the product is 8.
Syntax
Following is the basic syntax of the Python math.exp2() method −
math.exp2(x)
Parameters
This method accepts a real number, either an integer or a floating-point number as a parameter, representing the exponent to which 2 will be raised.
Return Value
The method returns the value of 2 raised to the power of x. The return value is a floating-point number.
Example 1
In the following example, we calculate 2 raised to the power of 3, i.e. passing a positive integer exponent to the base 2 as a parameter −
import math
result = math.exp2(3)
print("The result obtained is:", result)
Output
The output obtained is as follows −
The result obtained is: 8.0
Example 2
Here, we pass a negative integer exponent to the base 2 as a parameter. We calculate 2 raised to the power of -2, which is equivalent to 1 divided by 2 squared −
import math
result = math.exp2(-2)
print("The result obtained is:", result)
Output
Following is the output of the above code −
The result obtained is: 0.25
Example 3
In this example, we are passing a fractional exponent as a parameter to the base 2. We are calculating 2 raised to the power of 1.5 −
import math
result = math.exp2(1.5)
print("The result obtained is:", result)
Output
We get the output as shown below −
The result obtained is: 2.8284271247461903
Example 4
Now, we use a variable "x" to store the exponent value. We then calculate 2 raised to the power of "x", which is 22, resulting in 4 −
import math
x = 2
result = math.exp2(x)
print("The result obtained is:", result)
Output
The result produced is as shown below −
The result obtained is: 4.0
python_maths.htm