Python Practice › Loop Programs › Program 28
Power using a loop
Write a Python program to calculate base raised to a power using a for loop.
Input
base = 3, power = 4
Output
3^4 = 81 | check: 81
Approach
- Multiply a result by the base
powertimes.
Solution
base, power = 3, 4result = 1for _ in range(power): result *= baseprint(f"{base}^{power} = {result}", "| check:", base ** power)Output
3^4 = 81 | check: 81
Line by line
Line 3
The loop body runs exactly power times.
Learn the concept
New to this topic? Read the lesson, then come back.