How do you raise a number to a power in python?

How do you raise a number to a power in python?

To raise a number to a power in Python, use the Python exponent ** operator.

For example,23 is calculated by:

2 ** 3

And generally n to the power of m by:

n ** m

Python exponent is also related to another similar topic. The Python exponent notation is a way to express big or small numbers with loads of zeros. You can use the exponent notation e or E to replace the powers of ten.

For example, a billion (1 000 000 000) is 109. This means it can be written with an exponential notation using the letter e or E followed by the number of zeros:

  # Hard to read the zeros
1e09        # Easier to read

What Is an Exponent in Maths

An exponent is the number of times the number is multiplied by itself. In maths, the exponent is denoted with a number as a superscript, such as 23.

The operation involving exponents is called raising a number to a power.

This means number 2 is multiplied by itself 3 times. This gives: 23 = 2 * 2 * 2 = 8

That’s it for the maths part. Let’s see how to calculate exponents in Python.

Let’s also get to know the exponent notation, which can help you represent large and small numbers.

There are three ways you can raise a number to a power in Python:

  1. The ** operator
  2. The built-in pow() function
  3. The math module’s math.pow() function

Let’s go through each of these with examples. We are also going to discuss the subtle differences between these methods.

1. The Double Asterisk (**) Operator

You can use the double-asterisk operator to raise a number to a power in Python.

For example:

2 ** 3 # -> 8

This is a clear and efficient way to compute powers in Python. Most of the time, this approach is the fastest to compute power in Python. More on efficiency later.

2. Pow() Function

You can also use the built-in pow() function to raise a number to a power.

For instance:

pow(2, 3) # -> 8

3. Math.pow() Function

Finally, you may also use math.pow() function to raise a number to a power. Just remember to import the math module into your project.

For example:

import math

math.pow(2, 3) # -> 8.0

This function does the same thing as the two earlier power calculating approaches. But it is most efficient with floats.

Pow() vs math.pow() vs ** Operator

There are three main ways to raise a number to a power in Python. Let’s discuss the differences between them.

All three approaches work almost identically with one another. But there are some slight differences you may be interested to learn.

  1. ** is generally faster.
  2. Math.pow() uses floats only.
  3. Math.pow() does not allow imaginary numbers.
  4. The built-in pow() function accepts a third argument.

Let’s go through each of these main differences in a bit more detail.

1. ** Is Usually Faster

The double-asterisk approach is slightly faster than pow() or math.pow(). This is because it does not involve a separate function call.

For example:

from timeit import timeit

asterisk_time = timeit('70. ** i', setup='i = 10')
pow_time = timeit('pow(70., i)', setup='i = 10')
math_pow_time = timeit('math.pow(70, i)', setup='import math; i = 10')

print(f" **: {asterisk_time} \n pow: {pow_time} \n math.pow: {math_pow_time}")

Output:

 **: 0.066251788 
 pow: 0.080007215 
 math.pow: 0.10603947999999999

2. Math.pow() Only Uses Floats

Math.pow() handles its arguments differently compared to the built-in pow() function or ** operator. Math.pow() converts the arguments to floats and returns the result as a float. In comparison, the built-in pow() and the ** return the result as an integer with integer inputs.

math.pow(4, 2) # 8.0
pow(4, 2)      # 8
4 ** 2         # 8

If you want to raise a number to a power and have the result as a float, you can use math.pow(). This way you don’t have to separately convert the result to float yourself. This is very subtle, but the difference is there.

3. Math.pow() Does Not Accept Imaginary Numbers

The built-in pow() function and the ** operator support imaginary numbers. But math.pow() does not.

For instance:

pow(2, 1 + 0.5j)       # 1.8810842093664877+0.679354250205337j
2 ** 1 + 0.5j          # 1.8810842093664877+0.679354250205337j
math.pow(2, 1 + 0.5j)  # TypeError: can't convert complex to float

The math.pow() throws an error with imaginary units. So if you want to deal with imaginary numbers with powers, use pow() or **.

4. Pow() Takes a Third Argument

The built-in pow() function has a special use case for computing ab mod c. To do this, pass a third argument to pow() call.

For example, let’s compute 32 mod 4:

pow(3, 2, 4) # returns 1

It turns out this approach is faster than using the ** operator for doing the same:

(3 ** 2) % 4

Let’s make a comparison by using the timeit module and let’s use some big numbers:

import timeit

start = timeit.default_timer()
pow(3000000000, 2000, 4000)
stop = timeit.default_timer()

pow_time = stop - start

print('Time using pow: ', pow_time)

start = timeit.default_timer()
(3000000000 ** 2000) % 4000
stop = timeit.default_timer()

asterisk_time = stop - start

print('Time using **: ', asterisk_time)

print(f"The pow was {asterisk_time / pow_time} times faster")

Output:

Time using pow:  4.804000000000613e-06
Time using **:  0.0005024339999999995
The pow was 104.58659450456607 times faster

All in all, the differences between pow(), math.pow(), and ** are subtle but they exist. If you are a beginner, it does not really matter which approach you use as long as it works.

Python Exponent Notation—Get Rid of Zeros

Now that you know how to raise numbers to powers in Python, it’s time to learn what the exponent notation means in Python.

The exponent notation becomes handy with numbers that have a lot of digits.

For example, a big number like one billion (1 000 000 000) can be hard to read in Python. This is where the exponent notation helps. It lets you replace powers of ten with e or E.

As you may know, a billion is 109. So you can replace that in Python with a shorthand of 1e09. (This reads 1 multiplied 10 to the power of 9, which is billion.)

# Hard to read


# Better (possible for Python 3.6 <)
1_000_000_000

# Even better especially for the mathematically oriented
1e09

# or
1E09

The same goes for tiny numbers. Small numbers such as 0.7 or 0.03 are easy to read. But when numbers get really small, they become hard to read due to the leading zeros. For example, 0.000000001.

To overcome the readability issues, you can denote small numbers with the exponent notation e or E as well.

Let’s take a look at that number 0.000000001. Based on the number of zeros, it appears to be one billionth.

To denote this number in an exponent form, you can use the e notation. This time, however, you need to use a negative exponent (as the number is less than 1). This means billion becomes 1e-09.

# Hard to read
0.000000001

# Better
1e-09

Here are some random examples of representing numbers in the exponential form:

10
1e01

0.1
1e-01

2000
2e03

0.000342
3.42e-04
# Or just as well
342e-06

# The exponent notation does not always make things more readable, though.
# For example:
12000.15
1.200015e04

Conclusion

Python exponent refers to two things:

  • Raising numbers to a power.
  • Denoting large or small numbers with e or E to reduce zeros.

To raise a number to a power in Python, you can use the double-asterisk operator **:

2 ** 3

Or you can use the built-in pow() function:

pow(2, 3)

Or you can use the math.pow() function especially when dealing with floats:

import math

math.pow(2, 3)

To denote a number in the exponent form, replace powers of 10 with e or E:

# Hard to read


# Better (possible for Python versions of 3.6+)
1_000_000_000

# Even better especially for the mathematically oriented
1e09

# or
1E09

Thanks for reading. I hope you enjoy it.

Happy coding!

Further Reading

50 Python Interview Questions and Answers

50+ Buzzwords of Web Development

How do you raise to the power of 2 in Python?

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: **.

How do you do powers in Python?

Power (exponent) operator The operator that can be used to perform the exponent arithmetic in Python is ** . Given two real number operands, one on each side of the operator, it performs the exponential calculation ( 2**5 translates to 2*2*2*2*2 ).

How do you print a number to a power in Python?

How to find the power of a number in Python.
import math. print(math. pow(4,2)) Run. Importing math module in Python..
def power(n,e): res=0. for i in range(e): res *= n. return res. print(pow(4,2)) Run. ... .
def power(n, e): if e == 0: return 1. elif e == 1: return n. else: return (n*power(n, e-1)).