Hone

Lessons · Python · ** raising to a power

Raising to a power

a ** b raises a to the power b: 2 ** 3 is 8. It is not multiplication.

Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.

What it is for

Compound interest, areas and volumes, how many combinations a four-digit PIN has, the size of a bit pattern: powers, not products.

How to think about it

Say it in words before you type it: 'two to the three' is 2 ** 3, 'two times three' is 2 * 3. A fractional exponent gives a root: 9 ** 0.5 is 3.0.

Worked example

print(2 ** 3)
8.
print(2 * 3)
6: a different operation.
print(10 ** 4)
10000: how many four-digit PINs exist.
print(9 ** 0.5)
3.0: a half power is a square root.

Your turn

Area of a square with side s.

area = s  2

The trap

-2 ** 2 is -4, because ** binds tighter than the minus sign. Write (-2) ** 2 when you mean the square of negative two.

Practise ** raising to a power on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.