Code Explanation:
1. Importing partial from functools
from functools import partial
This imports the partial function.
partial is used to pre-fill some arguments of a function, creating a new version with fewer parameters.
2. Defining a Function multiply(x, y)
def multiply(x, y):
return x * y
This is a simple function that takes two arguments x and y, and returns their product (x * y).
3. Creating a New Function with partial
double = partial(multiply, y=2)
Here, partial creates a new function called double.
It uses multiply but pre-fills the argument y=2.
So double(n) is now equivalent to multiply(n, 2) → i.e., it doubles the number.
4. Calling the double Function
print(double(5))
This calls double(5), which internally does multiply(5, 2) → result is 10.
The result 10 is printed.
Final Output
10
Download Book - 500 Days Python Coding Challenges with Explanation


0 Comments:
Post a Comment