By now, you probably know how to multiply and divide numbers in Python. Multiplication in Python is fairly simple and easy to do. But what about using exponents? How would you raise a number to the second power, for example? If you're not sure, you'll probably find the answer pretty straightforward. To raise a number to the power of another number, you need to use the "**" operator. Where multiplying two numbers only uses one * symbol, the operator for raising one number to the power of another uses two: **.
Let's see an example. To find 4 squared (4 raised to the power of two is another way of saying it), your code would look like this:
4**2
Easy, right?
To print the results of the equation above, don't forget to use the print command.:
print(4**2)
The output of the code would be:
16
The snippet below will give you an example of how we would use exponents in a real context. In the snippet, we raise two to the power of the numbers 05 using an anonymous function (lambda), and print the results.
squares = 5 result = list(map(lambda x: 2 ** x, range(terms))) for i in range(squares): print("2 raised to the power of",i,"is",result[i])
So the output of the snippet above would be:
2 raised to the power of 0 is 1 2 raised to the power of 1 is 2 2 raised to the power of 2 is 4 2 raised to the power of 3 is 8 2 raised to the power of 4 is 16 2 raised to the power of 5 is 32
To use the snippet for yourself and get different results, change the value of the squares variable (this will give you more or less results, depending on how big or small the number is that you choose), or change the 2 value to another number. If you're looking for a way to understand how to handle exponents properly in Python, this code snippet is a great option for exploring that skill.
It's interesting to note that you can use the ** operator with imaginary numbers.
In Python, imaginary numbers are represented with the j notation. The language's support for complex numbers means you can effortlessly use the ** operator to raise them to various powers.
For instance, let's consider the imaginary unit, j (equivalent to the square root of 1). If you wish to square it, you'd utilize the ** operator as follows:
print(result) 
This code outputs:
(1+0j) 
The output showcases that the square of the imaginary unit is 1, which is a fundamental property of imaginary numbers.
You can also combine real and imaginary parts and raise them to real or imaginary powers. Let's take the number 3+4j:
complex_number = 3 + 4j result = complex_number ** 2 print(result) 
The code outputs:
(7+24j) 
There are two other ways you can calculate the exponents of numbers in Python.
Using the pow() Function to Calculate Exponents in Python
The builtin pow() function is an efficient way to calculate powers in Python.
While the "**" operator is intuitive and clear, pow()is the more traditional method. Furthermore, it is often more readable, especially for those new to Python programming or who are from a nonPython programming background.
As you'd expect, the function requires two arguments: the base and the exponent. If you wish to calculate 4 raised to the power of 3, you'd use the pow() function like this:
print(result)

And the output of the code would be:
64 
A unique feature of the pow()function is its third optional argument, which lets you calculate the power and then get the modulus of the result with a specified number.
For instance, pow (4,3,5) would give you the result of 43 mod 5.
Like the ** operator, the builtin pow() function seamlessly handles imaginary or complex numbers. This allows for an extensive range of operations on complex numbers without resorting to specialized mathematical libraries.
To raise an imaginary number using pow(), you simply provide the base and exponent as you would with real numbers:
result = pow(1j, 2)
print(result)

This, again, would give the output:
(1+0j) 
For more intricate calculations involving both real and imaginary parts, you can proceed similarly:
complex_base = 2 + 3j result = pow(complex_base, 2) print(result) 
The output of this code is:
(5+12j) 
Using math.pow() to Calculate Exponents in Python
Within Python's math library, there's also a math.pow() function, which is designed to work with floatingpoint numbers. This can be particularly helpful if you're working with noninteger bases or exponents and require more precision.
To use math.pow(), you first need to import the math module like so:
import math 
After importing, you can call the function:
result = math.pow(4, 2.5)
print(result)

Running this code gives the output:
32.0 
It's worth noting that math.pow() always returns a floatingpoint value, even if the result is a whole number. So, even if you were to calculate something like math.pow(4, 2), the result would be 16.0 instead of 16.
Unlike the pow() function, the math.pow() function does not accept a third argument. It's also interesting to note that the math.pow() function does not accept imaginary numbers.
If you attempt to compute the power of an imaginary number using math.pow(), Python will throw a TypeError. Let's see an example:
import math try: result = math.pow(1j, 2) print(result) except TypeError as e: print(f"An error occurred: {e}") 
Executing this code would give you the output:
An error occurred: can't convert complex to float 
This error indicates that the math.pow() function isn't equipped to handle complex numbers and strictly expects real numbers (floats) as arguments.
If you primarily work with real numbers and require the precision of floatingpoint calculations, math.pow() remains an excellent choice.
However, for operations involving imaginary or complex numbers, it's advisable to rely on the native ** operator or the builtin pow() function. This ensures accurate results and avoids unexpected errors in your code.