Python: Create a function that takes one argument multiplied with an unknown given number
Python Lambda: Exercise-2 with Solution
Write a Python program to create a function that takes one argument, and that argument will be multiplied with an unknown given number.
Sample Solution:
Python Code :
# Define a function 'func_compute' that takes a parameter 'n' and returns a lambda function
# The returned lambda function multiplies its argument 'x' by 'n'
def func_compute(n):
return lambda x: x * n
# Assign the result of calling func_compute with argument 2 to the variable 'result'
result = func_compute(2)
# Print the result of calling the lambda function stored in 'result' with argument 15
print("Double the number of 15 =", result(15))
# Reassign 'result' with the result of calling func_compute with argument 3
result = func_compute(3)
# Print the result of calling the lambda function stored in 'result' with argument 15
print("Triple the number of 15 =", result(15))
# Reassign 'result' with the result of calling func_compute with argument 4
result = func_compute(4)
# Print the result of calling the lambda function stored in 'result' with argument 15
print("Quadruple the number of 15 =", result(15))
# Reassign 'result' with the result of calling func_compute with argument 5
result = func_compute(5)
# Print the result of calling the lambda function stored in 'result' with argument 15
print("Quintuple the number 15 =", result(15))
Sample Output:
Double the number of 15 = 30 Triple the number of 15 = 45 Quadruple the number of 15 = 60 Quintuple the number 15 = 75
Explanation:
In the exercise above func_compute(n) takes an argument 'n' and returns a lambda function that multiplies its input (x) by the value of 'n'.
- result = func_compute(2): Calls 'func_compute' with an argument of 2 and assigns the returned lambda function to the variable 'result'.
- print("Double the number of 15 =", result(15)): Calls the lambda function stored in 'result' with an argument of 15, effectively doubling the number 15 and prints the result.
Then, the same process is repeated with different values for n:
- result = func_compute(3): Calls 'func_compute' with an argument of 3 and assigns the returned lambda function to 'result'.
- print("Triple the number of 15 =", result(15)): Calls the lambda function stored in 'result' with an argument of 15, tripling the number 15 and prints the result.
This sequence continues for 4 and 5.
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Python program to create a lambda function that adds 15 to a given number passed in as an argument, also create a lambda function that multiplies argument x with argument y and print the result.
Next: Write a Python program to sort a list of tuples using Lambda.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://198.211.115.131/python-exercises/lambda/python-lambda-exercise-2.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics