How does it works | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How does it works

Just going through some lessons on python course and I get stuck at this code Nums=(55,44,33,22) print (max(min(Nums[:2],abs(-42)))) I Don't Understand how it works I only know it prints 44 because (max(min(Nums[:2]))) But it why it's ignore abs(-42)

11th Sep 2021, 6:26 PM
Youcef Tahri
4 Answers
+ 3
Are you sure you copied the code correctly. In particular the parentheses. If this is the code print(max(min(Nums[:2]), abs(-42))) the explanation would be as follows: 1. The minimum of the first two values of Num is 44. 2. The maximum of 44 and 42 is 44.
11th Sep 2021, 6:32 PM
Simon Sauter
Simon Sauter - avatar
+ 3
i think this needs a bit more explanations: the initial code that is shown here does not work properly if you try to run it. the function min() gets 2 arguments: (min() function can have multiple arguments, but must be of same data type) Nums[:2] which is => a tuple: (55, 44) abs(-42) which is => an integer: 42 if they are now used as arguments for the min() function, it creates an exeption, because data type is not the same . so this expression 'abs(-42)'' should be converted to a tuple: '(abs(-42),)'' as both arguments are now tuples, the function min() works properly: 42 is the smallest number out of 55, 44, 42 now max() function is used and gets 42 as argument, and returns also 42. getting just only one number, max() function has no effect. so the result is 42. Nums=(55,44,33,22) # next 2 lines gives the same result print (max(min(Nums[:2],(abs(-42),)))) print (*min(Nums[:2],(abs(-42),)))
11th Sep 2021, 9:03 PM
Lothar
Lothar - avatar
0
Oh I got it I didn't notice that abs(-42) is inside max() i got it now thanks
11th Sep 2021, 6:34 PM
Youcef Tahri
0
I checked. As I suspected the parentheses were wrong and the code is actually print(max(min(Nums[:2]), abs(-42))) (It's the last question of the module quiz 12.1 in the Python Data Structures course.)
11th Sep 2021, 9:30 PM
Simon Sauter
Simon Sauter - avatar