Arithmetic Operators
Exponentiation means raising a base value to a power by repeated multiplication. The exponent tells you how many copies of the base are multiplied together. In 4 ** 3, the base is 4 and the exponent is 3, so the calculation is 4 × 4 × 4, which gives 64.
In Python 3, exponentiation is written with **. The base comes first, then the two asterisks, then the exponent. For example, 6 ** 2 means 6 × 6 and gives 36. Because the exponent counts copies of the base, 6 ** 2 uses two 6s, not one.
Do not use ^ for exponentiation in Python 3. Even though ^ is sometimes used for powers in maths, Python does not treat it as a power operator. To calculate 2 × 2 × 2 and get 8, you must write 2 ** 3, not 2 ^ 3.
Each fold of a sheet of paper doubles the number of layers, so the number of layers is 2 multiplied by itself once for every fold — 2 raised to the power of the number of folds. This program asks for the number of folds and works out the layers using **.
folds = int(input('Enter number of folds: '))
layers = 2 ** folds
print(layers)Output when the user types 5:
Enter number of folds: 5
32 Edexcel GCSE Computer Science · Arithmetic Operators
Edexcel GCSE Computer Science · Arithmetic Operators