Menu

Arithmetic Operators

Exponentiation

Exponentiation means raising a number to a power: a number is multiplied by itself again and again. In Python you do it with the power operator **, written between the base (the number being multiplied) and the exponent (how many copies of the base are multiplied together).

So 4 ** 2 gives 16, because 4 × 4 = 16, and 3 ** 3 gives 27, because 3 × 3 × 3 = 27. The value on the left is the number you start with; the value on the right tells you how many of them to multiply.

Squaring and cubing

  • An exponent of 2 is called squaring, because it gives the area of a square: 9 ** 2 gives 81.
  • An exponent of 3 is called cubing, because it gives the volume of a cube: 4 ** 3 gives 64.
OperationResultWhy
6 ** 2366 × 6
6 ** 32166 × 6 × 6
10 ** 41000010 × 10 × 10 × 10

Exponentiation is useful whenever the same value is multiplied over and over. A four-digit PIN chosen from the digits 0 to 9 has 10 000 possible codes, because each of the four positions can be any of ten digits: 10 ** 4 = 10 × 10 × 10 × 10.

The symbol matters

The power operator is ** and not ^. The ^ symbol is a different operator that does a different job, so 6 ^ 2 does not mean “six squared” in Python and will not give you 36. Always type two asterisks side by side.

hours = int(input("How many hours has the colony grown for? "))
print(2 ** hours)   # bacteria after that many hours

Output when the user types 6:

How many hours has the colony grown for? 6
64

The exponent does not have to be typed as a number. A colony of bacteria that doubles every hour has 2 ** hours bacteria, so the program raises 2 to whatever value the user enters, and the answer grows very quickly.

Practise this