How do I find the maximum of 2 numbers?

Viewed 233476

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?

12 Answers
# Python 3
value = -9999
run = int(input())

maxnum = run if run > value else value
print(maxnum)

There are multiple ways to achieve this:

  1. Custom method
def maximum(a, b):
if a >= b:
    return a
else:
    return b
 
value = -9999
run = problem.getscore()
print(maximum(value, run))
  1. Unbuilt max()
value = -9999
run = problem.getscore()
print(max(value, run))
  1. Use of ternary operator
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()

Related