Editor

Create a Percentage Calculator with partial

Intermediate · Write the code · from Python functools: lru_cache, partial, reduce, singledispatch

From the lesson: Python functools: lru_cache, partial, reduce, singledispatch

Imagine your functions could remember past answers, come pre-loaded with some arguments, or automatically handle different data types. That's what the functools module gives you: power-ups for your functions.

Read the lesson

Given the function calculate(amount, rate) that returns amount * rate / 100, use partial to create a tax_15 function that always uses rate=15. Print tax_15(200) and tax_15(500).

from functools import partial
def calculate(amount, rate):
return amount * rate / 100
# Create tax_15 using partial
# Print tax_15(200) and tax_15(500)

More functools exercises

All Python exercises · 50 practice problems · Standard Library Essentials quiz