How to Calculate Exponents in Python

Learn to use the exponentiation operator

How to Calculate Exponents in Python
Photo by recha oktaviani on Unsplash

Arithmetic with exponents used to be an unsuspecting hassle. Legacy languages or even older versions of more current languages required standalone functions or even imported methods to raise a number to a given power.

For example, in C++ we’ll need to include the math.h library and use the pow() function. Similarly, older version of PHP use the pow() function, albeit without import. If you’ve worked with JavaScript you may have seen the Math.pow() method.

Granted many older languages have since implemented improvements for exponentiation, Python has been silky smooth since the beginning.

Exponentiation Operator in Python

Raising a number to an exponent in Python is as simple as using the exponentiation operator, a double asterisk.print(2**3) # 8

This simple operator is natively available and easy to remember (think of exponentiation as super multiplication).

The beautiful elegance of a standalone operator is that exponents can be easily written inline with other arithmetic without sacrificing readability.result = 10**(4+2)
print(result) # 1000000

Word of Caution

A quick note of care, while the exponentiation operator is simple and easy-to-use, it can easily be the victim of mistaken identity because the asterisk symbol is a multi-purpose operator in Python. Here’s a quick list of other asterisk implementations to be mindful of:

  • Multiplication (2*3): The arithmetic operator we all know and love
  • Splat Operator (*): Used to unpack arguments such as a list or tuple
  • Splat Operator (**): Used to unpack keyword arguments such as a dictionary

Please share your experiences, questions, and feedback below. Follow Code 85 for more plain language programming guides. Thanks for reading!

Subscribe to Dreams of Fortunes and Cookies

Sign up now to get access to the library of members-only issues.
Jamie Larson
Subscribe