Python Practice › Loop Programs › Program 28

Power using a loop

EasyLoop Programs · 28 of 90

Write a Python program to calculate base raised to a power using a for loop.

Advertisement
Input
base = 3, power = 4
Output
3^4 = 81 | check: 81

Approach

  1. Multiply a result by the base power times.

Solution

Python
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.

More loop programs

← All 90 Loop Programs

Leave a Comment