+ 2
can somebody explain this question to me in easy language
def add(x, y): return x + y def do_twice(func, x, y): return func(func(x, y), func(x, y)) a = 5 b = 10 print(do_twice(add, a, b))
4 Réponses
+ 3
Ya sure NIKHIL
def add(x ,y):
return x + y
You have created a function name add with 2 parameters and you are returning the sum of those 2
def do_twice(func, x, y):
return func(func(x, y), func(x, y))
You have created a function called do twice which accepts 3 parameters
[ IMPORTANT ]
How to assign a function in a variable
>>> p = print #note we haven't called function by using ()
>>>
>>> p("Hello, World")
Hello, World
>>> k = list
>>> k()
[]
Now same with your case,
a = 5
b = 10
print(do_twice(func=add, x=a, y=b))
in your function now whenever you write func() it will only call add, means its similar to
add(add(5, 10), add(5, 10))
add(15, 15)
= 30
Hope it's helpful!
Happy Learning 🙂
Example: https://code.sololearn.com/cM8gk3xKaF5F/?ref=app
0
NIKHIL
Explanation is here
https://code.sololearn.com/cgfonDJu8pH0/?ref=app
- 1
You really don't need to understand this code, cz you can double the value in multiple ways, i always prefer to use while and for loops. because functions are bit slow