Python Program to find Maximum of Two Numbers With Examples

In this article, we will learn how to write python program to find maximum of two numbers with examples. We will see different examples using if-else condition, some built-in function etc.

Getting Started

The task is to find greatest or largest among two numbers using python program. For example,

If a = 10 and b = 30, then output should be b = 30

But, we have to do it using python program.

We can write python program to find greatest in many ways as shown below –

  1. Using if-else Condition
  2. Using if-else Condition – Single Line
  3. Using max() Function
  4. Using sorted() Function
  5. Using fmax() Function From math Module

Using if-else Condition

This is simplest way to check greatest of 2 numbers in python program. We will use if-else condition to find it.

def findLarger(num1, num2):
    if num1 > num2:
        return num1
    else:
        return num2

num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
result = findLarger(num1, num2)
print(f"The larger number is: {result}")

Here,

  • We have used simple if-else condition if num1 is greater than num2 or not using “>” operator.
  • if num1 is greater than num2, num1 is returned from findLarger method, else num2 is returned.
  • Finally, we are storing the returned value into result variable and printing it using print() function.

Output:

Enter the first number: 
43.3
Enter the second number: 
23.6
The larger number is: 43.3

Using if-else Condition – Single Line

We can simplify above program and make it if-else condition into single line as shown below –

num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
result = num1 if num1 > num2 else num2
print(f"The larger number is: {result}")

Here,

  • We have simplified above findLarger() method into single line – num1 if num1 > num2 else num2
  • But, it works similar as earlier.

Output:

Enter the first number: 
32.8
Enter the second number: 
45.9
The larger number is: 45.9

Using max() Function

If we don’t want to use if-else condition to check maximum number, python provides some built-in methods as well to find largest of two numbers in python. For example, max() method in python.

We can write python program to find largest of two numbers using max() method as shown below –

num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
result = max(num1, num2)
print(f"The larger number is: {result}")

Here,

  • max() returns maximum of two numbers which are being stored in result variable.
  • Finally, we print it using print() function

Output:

Enter the first number: 
5.0
Enter the second number: 
34.9
The larger number is: 34.9

Using sorted() Function

To find largest of two number, you can use if-else or max() function. That’s simplest way to achieve our task.

There is another way to do it – using sorted() function.

We can write python program to find greatest of two numbers using sorted() function too as shown below –

num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
result = sorted([num1, num2])[-1]
print(f"The larger number is: {result}")
  • sorted() function sorts the number in ascending order. Then, we get last number using [-1] indexing.
  • Finally, we store the returned value in result and print it using print() function.

Output:

Enter the first number: 
45.3
Enter the second number: 
87.9
The larger number is: 87.9

Using fmax() Function From math Module

We can also using fmax() function from math module to find maximum of two numbers in python.

import math

num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
result = math.fmax(num1, num2)
print(f"The larger number is: {result}")

Output:

Enter the first number: 
45.3
Enter the second number: 
87.9
The larger number is: 87.9

That’s how we can write python program to find maximum of two numbers using if-else condition, max() function, fmax() function and sorted() function.

Learn about python at official Doc

Leave a Reply