Skip to content
Exercise · Functions · lesson 7 of 8

Exercise: power by recursion

Write a recursive function power(base, exp) that returns base raised to the power exp. Do not use the ** operator.

  • power(2, 10) → 1024
  • power(-2, 3) → -8
  • power(7, 0) → 1

Constraints: base is an integer from -10 to 10, zero and negative values included. exp is an integer from 0 to 50; any number to the power 0 is 1, including power(0, 0).

Do not add extra imports unless the task says so explicitly.

power.py — python
python · 3 lines
Ctrl+Enter run · Esc leave editor

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way