Explanation:
Step 1: Import partial
from functools import partial
- partial is a function from Python's functools module.
- It allows us to create a new function by fixing one or more arguments of an existing function.
Step 2: Create a Partial Function
f = partial(pow, 3)
- pow() normally accepts two or three arguments:
pow(base, exponent)pow(base, exponent, modulus)
- Here, partial(pow, 3) fixes the first argument (base) as 3.
- Now, f() behaves like this:
f(exponent)
is equivalent to:
pow(3, exponent)
Step 3: Execute the Function
print(f(2), f(2, 5))
First call:
f(2)
Equivalent to:
pow(3, 2)
Calculation:
\[
3^2 = 9
\]
Second call:
f(2, 5)
Equivalent to:
pow(3, 2, 5)
Calculation:
\[
3^2 \bmod 5 = 9 \bmod 5 = 4
\]
Final Output
9 4
.png)

0 Comments:
Post a Comment