0

logic behind this:

def max(x, y): if x >=y: return x else: return y print(max(4, 7)) z = max(8, 5) print(z) Can someone please explain this code line to line

20th Jun 2020, 11:19 AM
Anil Gurjar
Anil Gurjar - avatar
4 Antworten
+ 4
Anil Gurjar def is a identifier which use to define function so here max is a function which has two parameters x,y and returns max value. Here if x will be greater than or equal to y then max will be x otherwise y So for this max(4, 7) max value will be 7 for max(8, 5) max value will be 8 Here z = max(8, 5) which will assign max value to another variable z
20th Jun 2020, 11:31 AM
A͢J
A͢J - avatar
+ 6
Anil Gurjar, please avoid using names like max that are also used for python builtin functions *max()*. In some cases this can lead to errors, that are really hard to catch.
20th Jun 2020, 12:00 PM
Lothar
Lothar - avatar
+ 5
in print(max(4,7)) you are printing the returned value when function is called in z=max(8,5) z stores the value returned by calling function and after you print z(it's value)
20th Jun 2020, 11:29 AM
Abhay
Abhay - avatar
+ 1
The function is returning a number which is greater than the other. But if x and y are both equal then it will return x. Now, let's go to the second half, Since, the function is just returning a number We can either assign it a variable or directly print it: x = 5 print(x) And print(5) Are the same thing! I hope this helped you! If you still find any problem pls let me know
20th Jun 2020, 11:29 AM
Namit Jain
Namit Jain - avatar