Exponents
An exponent is shorthand for repeated multiplication: instead of writing you write . This lesson covers the vocabulary, how to read and compute powers by hand and in Python, and the sign traps that catch people (and programs).
Base and exponent
In the base is 3 and the exponent (or power) is 4. The exponent says how many copies of the base are multiplied together:
You read as "three to the fourth power" or "three to the fourth". Two powers have their own names that come from geometry:
| Expression | Read as | Why |
|---|---|---|
| five squared | area of a 5-by-5 square | |
| five cubed | volume of a 5-by-5-by-5 cube | |
| five to the first | just 5 itself |
An exponent of 1 means "one copy", so any number to the first power is itself: .
Powers in Python
Python writes powers with ** (two stars). The ^ symbol does not mean "power" in Python, JavaScript, C or Java; it is the bitwise XOR operator, a classic beginner bug.
print("3 ** 4 =", 3 ** 4)
print("2 ** 5 =", 2 ** 5)
print("10 ** 3 =", 10 ** 3)
print("pow(7, 2) =", pow(7, 2))
print("1.5 ** 2 =", 1.5 ** 2)
# The trap: ^ is XOR, not power
print("3 ^ 4 =", 3 ^ 4, " (XOR, not 81!)")
# Python integers never overflow, so huge powers are exact
print("2 ** 100 =", 2 ** 100)3 ** 4 = 81
2 ** 5 = 32
10 ** 3 = 1000
pow(7, 2) = 49
1.5 ** 2 = 2.25
3 ^ 4 = 7 (XOR, not 81!)
2 ** 100 = 1267650600228229401496703205376
Note: Python's integers grow as large as memory allows, so
2 ** 100is exact. In C, Java or Rust a 32-bitintwould overflow long before that. See Integer Overflow and Two's Complement.
Negative bases and parentheses
Powers come before negation in the order of operations. That makes these two expressions different:
A negative base raised to an even power gives a positive result; to an odd power, a negative result. Python follows the same rule, which surprises people who type -3 ** 2:
print("-3 ** 2 =", -3 ** 2) # parsed as -(3 ** 2)
print("(-3) ** 2 =", (-3) ** 2)
print("(-2) ** 3 =", (-2) ** 3)
print("(-2) ** 4 =", (-2) ** 4)
for n in range(1, 7):
print(f"(-1)^{n} = {(-1) ** n:2d}")-3 ** 2 = -9
(-3) ** 2 = 9
(-2) ** 3 = -8
(-2) ** 4 = 16
(-1)^1 = -1
(-1)^2 = 1
(-1)^3 = -1
(-1)^4 = 1
(-1)^5 = -1
(-1)^6 = 1
The flip-flopping is a handy trick for alternating signs, for example in series that go .
Powers grow fast
Squares and cubes of small numbers are worth knowing by heart. Notice how quickly the columns run away from each other:
print(" n n² n³ 2ⁿ")
for n in range(1, 11):
print(f"{n:2d} {n**2:4d} {n**3:5d} {2**n:6d}") n n² n³ 2ⁿ
1 1 1 2
2 4 8 4
3 9 27 8
4 16 64 16
5 25 125 32
6 36 216 64
7 49 343 128
8 64 512 256
9 81 729 512
10 100 1000 1024
When the exponent is the thing that grows (), the numbers explode much faster than when the base grows ( or ). That difference is the heart of Growth Rates and Big-O.
Tip: To square a number ending in 5, take the leading digits , compute , and append 25. For : , so 1225.
In software
- Area and volume: a texture that is 512 pixels wide and tall has pixels; doubling the side quadruples the memory.
- Combinations: a 4-digit PIN has possibilities; a password of 8 lowercase letters has billion. That is why longer passwords are so much harder to brute-force.
- Easing curves in UI animation often use or for a value between 0 and 1 to make motion start slowly and speed up (see easing).
Exercises
Exercise 1: Expand and compute
Write each power as a product and compute it: , , , .
Answer
, , , .
Solution
Note that and are equal: , so .
Exercise 2: Signs
Compute , , and .
Answer
25, −25, −125, −1.
Solution
Exercise 3: PIN codes
A lock uses a code of 6 digits, each 0–9. How many codes are possible? How many more is that than a 4-digit code?
Answer
codes, which is 990,000 more than the four-digit codes (100 times as many).
Solution
Each of the 6 positions has 10 choices, and the choices multiply: .
six = 10 ** 6
four = 10 ** 4
print(six, four, six - four, six // four)1000000 10000 990000 100
Exercise 4: Find the exponent
Write a loop that finds the smallest with .
Answer
, because and .
Solution
Start at and keep increasing while is still too small.
n = 0
while 3 ** n <= 1_000_000:
n += 1
print(n, 3 ** (n - 1), 3 ** n)13 531441 1594323
Test yourself
What does 2 ** 3 ** 2 print in Python? (Hint: ** groups from the right.)
- 64
- 512
- 36
- 12
What is (without parentheses)?
- 16
- −8
- −16
- 8
Which Python expression computes 5 to the third power?
- 5 ^ 3
- 5 ** 3
- 5 * 3
- pow(3, 5)