Sling Academy
Home/Python/2 Ways to Calculate Exponents (Powers) in Python

2 Ways to Calculate Exponents (Powers) in Python

Last updated: June 18, 2023

Exponents are mathematical operations that represent the number of times a number (the base) is multiplied by itself. This short and straight-to-the-point article shows you 2 different approaches to calculating exponents in Python. Without any further ado, let’s get our hands dirty with code.

Using the ** operator

Like many other programming languages, Python uses the ** operator for exponentiation. The example below shows you how to use the ** operator to calculate the result of 10 to the power of 3:

output = 10 ** 5
print(output)

Output:

100000

10 is the base, and 5 is the exponent.

Here’s another example with the exponent is a float:

result = 10 ** 3.25
print(result)

Output:

1778.2794100389228

Using the math.pow() function

Besides the ** operator, you can use the pow() function of the math module to get the job done.

Example:

import math 

result1 = math.pow(2, 4)
result2 = math.pow(2, 1.5)

print('result1 =', result1)
print('result2 =', result2)

Output:

result1 = 16.0
result2 = 2.8284271247461903

Note that the pow() function always returns a float. In the example above, you get 16.0 instead of 16.

Previous Article: Python: How to Convert a Number to a Byte Array

Series: Python – Numbers & Math Tutorials

Python

You May Also Like

  • Introduction to yfinance: Fetching Historical Stock Data in Python
  • Monitoring Volatility and Daily Averages Using cryptocompare
  • Advanced DOM Interactions: XPath and CSS Selectors in Playwright (Python)
  • Automating Strategy Updates and Version Control in freqtrade
  • Setting Up a freqtrade Dashboard for Real-Time Monitoring
  • Deploying freqtrade on a Cloud Server or Docker Environment
  • Optimizing Strategy Parameters with freqtrade’s Hyperopt
  • Risk Management: Setting Stop Loss, Trailing Stops, and ROI in freqtrade
  • Integrating freqtrade with TA-Lib and pandas-ta Indicators
  • Handling Multiple Pairs and Portfolios with freqtrade
  • Using freqtrade’s Backtesting and Hyperopt Modules
  • Developing Custom Trading Strategies for freqtrade
  • Debugging Common freqtrade Errors: Exchange Connectivity and More
  • Configuring freqtrade Bot Settings and Strategy Parameters
  • Installing freqtrade for Automated Crypto Trading in Python
  • Scaling cryptofeed for High-Frequency Trading Environments
  • Building a Real-Time Market Dashboard Using cryptofeed in Python
  • Customizing cryptofeed Callbacks for Advanced Market Insights
  • Integrating cryptofeed into Automated Trading Bots