How do I find a maximum of 2 numbers?

How to find a maximum of 2 numbers?

value = -9999 run = problem.getscore() 

I need to compare 2 values ​​ie value and run and find a maximum of 2. Do I need a python function to work it?

+82
python max
Jul 28 '10 at 20:49
source share
10 answers

Use the built-in max function.

Example: max(2, 4) returns 4.

Just for a giggle, there is min ... if you need it. :P

+194
Jul 28 '10 at 20:50
source share
β€” -
+21
Jul 28 '10 at 20:50
source share

max(number_one, number_two)

+12
Jul 28 '10 at 20:50
source share

You can use max(value, run)

The max function takes any number of arguments or (alternatively) an iteration and returns the maximum value.

+8
Jul 28 '10 at 20:50
source share
 max(value,run) 

must do it.

+7
Jul 28 '10 at 20:50
source share

Just for fun, after the party is over and the horse is locked.

Answer: max() !

+4
Jul 28 2018-10-22T00:
source share

You can also achieve the same result using a conditional expression :

 maxnum = run if run > value else value 

slightly more flexible than max , but admittedly longer for input.

+3
Nov 23 '16 at 19:08
source share

I noticed that if you have units rounded to an integer, it would be better to use:

c=float(max(a1,...,an))/b

Sorry for posting late!

+2
Apr 25 '13 at 15:08
source share
 numberList=[16,19,42,43,74,66] largest = numberList[0] for num2 in numberList: if num2 > largest: largest=num2 print(largest) 

gives the largest number from the list of numbers without using the Max operator

+1
Jun 09 '16 at 0:05
source share

(num1>=num2)*num1+(num2>num1)*num2 will return a maximum of two values.

+1
Sep 22 '16 at 22:52
source share



All Articles