Power Function With Type Hints

Easy ⏱ 8 min 84% acceptance ★★★★☆ 4.4
Write a function power(base: float, exponent: int = 2) -> float that returns base raised to exponent, annotated exactly as shown (base is a float, exponent is an int defaulting to 2, return type is float). Type hints are documentation only in Python — your implementation must still behave correctly at runtime.

Examples

Example 1
Input
power(3.0)
Output
9.0
Explanation

Exponent defaults to 2, so 3.0 ** 2 = 9.0.

Example 2
Input
power(2.0, 3)
Output
8.0
Explanation

2.0 ** 3 = 8.0.

Constraints

  • Use the exact parameter and return type annotations described.

Topics

Functionstype hints

Companies

AppleIntel

Hints

Hint 1

Annotation syntax: def f(x: int, y: str = "a") -> bool:

Hint 2

The ** operator computes exponentiation.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.