Calculate the power of a number

Write a function named power that takes a number x and an integer exponent y as input and returns x raised to the power of y. Use a for loop rather than ** or pow. Remember that a negative exponent requires the reciprocal of the corresponding positive power.

Use finite numeric inputs. If x is 0, use a nonnegative exponent. For this exercise, exponent 0 returns 1, including when x is 0.

Complete the supplied function in Listing 1. Keep its signature and use the checks below it.

Listing 1: Starter function and supplied checks for calculate the power of a number.
from math import isclose


def power(x: float, y: int) -> float:
    # Add code here!
    pass


assert power(2, 3) == 8
assert power(5, 0) == 1
assert isclose(power(3, -2), 1 / 9)

range(start, stop) supplies integers beginning at start and ending before stop. For example, list(range(1, 4)) is [1, 2, 3]. An empty range supplies no values.

assert condition stops the check with an AssertionError when the condition is false. abs(value) returns its magnitude, so abs(-2) is 2. math.isclose(a, b) checks whether 2 numeric values are close within its tolerances; isclose(0.1 + 0.2, 0.3) is True with the default tolerances.

range(stop) supplies integers from 0 up to, but excluding, stop; list(range(3)) is [0, 1, 2].