alan
PRO
last month
Alancountry asked

What’s the correct way to do exponent calculations in Python? I tried using ^ because that’s what calculators use.

Udayan Shakya
Expert
last month
Udayan Shakya answered

Hi Alan, that’s a great question.

In Python, powers work a little differently from what you see on most calculators. Calculators usually use the ^ symbol for exponents, so it’s normal to try the same thing in Python. The tricky part is that Python doesn’t use ^ for powers at all, so it won’t give the result you expect.

The Python way to calculate a power is with two stars. So if you want “4 to the power of 3,” you write:

4 ** 3

Python reads that as “multiply 4 by itself three times,” which gives:

4 * 4 * 4 = 64

You can do the same with decimals:

2.5 ** 3   # 15.625

Once you get used to **, it becomes pretty straightforward.

If you have more questions while you’re learning, I’m happy to help.

Python
This question was asked as part of the Learn Python Basics course.