How to find the maximum of 2 numbers?
value = -9999
run = problem.getscore()
I need to compare the 2 values i.e value
and run
and find the maximum of 2. I need some python function to operate it?
Use the builtin function max
.
Example:
max(2, 4)
returns 4.
Just for giggles, there's a min
as well...should you need it. :P
max(number_one, number_two)
max(number_one, number_two)
. The []
's don't add anything useful.
Jul 28, 2010 at 20:51
You can use max(value, run)
The function max
takes any number of arguments, or (alternatively) an iterable, and returns the maximum value.
Just for the fun of it, after the party has finished and the horse bolted.
The answer is: max()
!
You could also achieve the same result by using a Conditional Expression:
maxnum = run if run > value else value
a bit more flexible than max
but admittedly longer to type.
I noticed that if you have divisions it rounds off to integer, it would be better to use:
c=float(max(a1,...,an))/b
Sorry for the late post!
numberList=[16,19,42,43,74,66]
largest = numberList[0]
for num2 in numberList:
if num2 > largest:
largest=num2
print(largest)
gives largest number out of the numberslist without using a Max statement
# Python 3
value = -9999
run = int(input())
maxnum = run if run > value else value
print(maxnum)
There are multiple ways to achieve this:
def maximum(a, b): if a >= b: return a else: return b value = -9999 run = problem.getscore() print(maximum(value, run))
value = -9999 run = problem.getscore() print(max(value, run))
value = -9999 run = problem.getscore() print(value if value >= run else run)
But as you mentioned you are looking for inbuilt so you can use max()