Arithmetic Operators
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.
9 ** 2 gives 81.4 ** 3 gives 64.| Operation | Result | Why |
|---|---|---|
6 ** 2 | 36 | 6 × 6 |
6 ** 3 | 216 | 6 × 6 × 6 |
10 ** 4 | 10000 | 10 × 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 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 hoursOutput 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.
OCR GCSE Computer Science · Arithmetic Operators
OCR GCSE Computer Science · Arithmetic Operations