Writing Lambda Functions A lambda function is an anonymous function. The syntax
ID: 3599582 • Letter: W
Question
Writing Lambda Functions A lambda function is an anonymous function. The syntax for writing a lambda function is straightforward enough variable-name lambda argument-one, argument-two : return-value = For example, we could write a simple lambda function which increments its argument. increment = lambda x: x + 1 We can then call this in exactly the same way as a normal function: increment (3) # increment (4.5) ->4 #-> 5.5 In this problem, we want you to write three simple lambda functions. They all should do what they say on the tin Write square(x), is_odd(x), and add (x, y) Write square(x), is_odd(x), and add(x, y) Submitted: No Status: Logged in square (2) - 4 is_odd (2)-> False is_odd (1)-> True add (1, 3)- 4 name 'square' is not defined You need to write a lambda for squareExplanation / Answer
the square(x) lambda function will be:
square = lambda x:x*x
We need to return the square of the number x which is x*x so the lambda function is correct as described above.
is_odd = lambda x: ( x%2 != 0)
We need to check if the number is odd or not. We can use the modulus operation for this. if number is odd then modulus will return 1 and it is != 0 so it will return true.
add = lambda x,y:x+y
We need to add two numbers so we will have two arguments x and y. The return for them is x+y which we are returning in the function.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.