What will be the output of the following Python code?def writer(): title = 'Sir' name = (lambda x:title + ' ' + x) return name who = writer()who('Arthur')
Question
What will be the output of the following Python code?
def writer():
title = 'Sir'
name = (lambda x: title + ' ' + x)
return name
who = writer()
who('Arthur')
Solution
The output of the given Python code will be 'Sir Arthur'.
Here's the step by step explanation:
-
The function
writer()
is defined. Inside this function, a variabletitle
is assigned the value 'Sir'. -
Then, a lambda function is assigned to the variable
name
. This lambda function takes one argumentx
and returns a string that concatenates thetitle
andx
with a space in between. -
The
writer()
function returns the lambda functionname
. -
The
writer()
function is called and its return value (which is the lambda functionname
) is assigned to the variablewho
. -
Finally, the lambda function stored in
who
is called with the argument 'Arthur'. This returns the string 'Sir Arthur'.
Similar Questions
10.What will be the output of the following Python code?string = "my name is x"for i in string.split(): print (i, end=", ")
What is the output of the following function calldef fun1(name, age=20): print(name, age) fun1('Emma', 25)Emma 25Emma 20Emma 22
What will be the output of the following Python code?def foo(k): k = [1]q = [0]foo(q)print(q)
What would be the output of the following Python statement?Dict = {'Rno' : '123' , 'Name' : 'Ajay' , 'Course': 'B.tech.'}print(Dict.get('Name'))
What will the following code print?name = "William";sum=0for i in range(len(name)): sum += iprint(sum)
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.