How to Do Exponents in Python
What you’ll build or solve
You’ll learn how to raise numbers to a power in Python using the two standard approaches.
When this approach works best
Exponents are a good fit when you:
Learn Python on Mimo
- Calculate growth and decay, like compound interest or population change.
- Work with geometry and physics formulas, like squaring a distance or cubing a volume.
- Transform values, like scaling scores or applying power-based formulas.
Avoid this approach when you need exact decimal math for money. Float-based results can look slightly off. Round for display, or use decimal.Decimal for finance-style precision.
Prerequisites
- Python installed
- You know what powers mean, like 2³
Step-by-step instructions
1) Pick how you want to raise a number to a power
Python gives you two common ways to do exponents.
Option A (most common): the exponent operator **
Python
print(2 ** 3) # 8
print(10 ** 2) # 100
Option B: the built-in pow() function
Python
print(pow(2, 3)) # 8
print(pow(10, 2)) # 100
Use ** when you want the clearest syntax in normal code. Use pow() when you prefer a function call.
2) Handle negatives and precedence correctly
Negative bases and operator order can trip people up.
If the base is negative, use parentheses:
Python
print((-4) ** 2) # 16
print((-4) ** 3) # -64
This is different:
Python
print(-4 ** 2) # -16
Python reads -4 ** 2 as -(4 ** 2) because ** runs before the unary minus.
Exponents also bind tighter than multiplication and addition:
Python
print(2 + 3 ** 2) # 11
print((2 + 3) ** 2) # 25
Use parentheses when you want a specific order.
3) Use fractional and negative exponents
Fractional exponents produce roots, and negative exponents produce reciprocals.
A square root using an exponent:
Python
print(9 ** 0.5) # 3.0
A cube root using an exponent:
Python
print(27 ** (1 / 3)) # may show a tiny rounding difference
A negative exponent:
Python
print(2 ** -3) # 0.125
Fractional powers use floats, so you may see small rounding noise. Round for display if needed.
Examples you can copy
Example 1: Compound growth (simple)
Python
principal = 1000
rate = 1.05
years = 3
final_amount = principal * (rate ** years)
print(final_amount)
Example 2: Area and volume (different powers)
Python
side = 4
area = side ** 2
volume = side ** 3
print(area)
print(volume)
Example 3: Reusable power calculation with input
Python
base = float(input("Base: "))
exponent = float(input("Exponent: "))
print(base ** exponent)
Example 4: Fast modular exponentiation with pow()
Use this when you need (base ** exponent) % mod for large numbers.
Python
base = 5
exponent = 117
mod = 19
print(pow(base, exponent, mod))
Example 5: Scale scores with an exponent
Python
raw_score = 82
max_score = 100
normalized = (raw_score / max_score) ** 2
print(normalized)
Example 6: Exact decimal squaring for money-like values
If you need exact decimal math, use Decimal instead of float.
Python
from decimal import Decimal
price = Decimal("19.99")
print(price ** 2) # 399.6001
Common mistakes and how to fix them
Mistake 1: Using ^ for exponentiation
What you might do:
Python
print(2 ^ 3)
Why it breaks: ^ is bitwise XOR in Python, not power.
Correct approach:
Python
print(2 ** 3)
print(pow(2, 3))
Mistake 2: Forgetting parentheses for negative bases
What you might do:
Python
print(-4 ** 2)
Why it breaks: Python applies ** before the unary minus, so the result becomes -(4 ** 2).
Correct approach:
Python
print((-4) ** 2)
Mistake 3: Expecting perfect results from fractional powers
What you might do:
Python
print(27 ** (1 / 3))
Why it breaks: Float math can produce a close result that prints with a tiny error.
Correct approach:
Python
value = 27 ** (1 / 3)
print(round(value, 10))
Troubleshooting
If you see TypeError: unsupported operand type(s) for **, convert inputs to numbers first with float(...) or int(...).
If you see ValueError while converting input, the text is not numeric. Try 2, 2.5, or -3.
If you get -16 from -4 ** 2, add parentheses: (-4) ** 2.
If you see results like 2.9999999999999996, round for display with round(result, 6).
If you need (a ** b) % m and it feels slow with large b, use pow(a, b, m).
Quick recap
- Use
**for exponents:base ** exponent. - Use
pow(base, exponent)as an alternative. - Put negative bases in parentheses:
(-4) ** 2. - Use fractional exponents for roots and negative exponents for reciprocals.
- Round float results if you see small precision noise.
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot