Find the Greatest of Two Numbers in Python

Find the Greatest of the Two Numbers in Python

Find the Greatest of the Two Numbers

Given two integer inputs as number1 and number2, the objective is to find the largest among the two. Therefore we’ll write a code to Find the Greatest of the Two Numbers in Python Language.

Example
Input : 20 40
Output : 40

Find the Greatest of the Two Numbers in Python Language

Given two integer inputs, the objective is to find the largest number among the two integer inputs. In order to do so we usually use if-else statements to check which one’s greater. Here are some of the Python methods to solve the above mentioned problem.

  • Method 1: Using if-else Statements
  • Method 2: Using Ternary Operator
  • Method 3: Using inbuilt max() Function

We’ll discuss the above mentioned methods in detail in the upcoming sections.

Method 1: Using if-else Statements

In this method we’ll find the Largest Number using simple if-else statements.

Working

For the two integer inputs num1 and num2

  • Check if num1>num2, print num1 if true.
  • Print num2 otherwise.

Let’s implement the above logic in Python Language.

Python Code

Run
num1, num2 = 20 , 30
if num1>num2:
  print(num1)
else:
  print(num2)

Output

30

Method 2: Using Ternary Operator

In this method we’ll use Ternary Operator in Python to find the Largest Number among the two input integers.

Working

Given the integer inputs num1 and num2

  • Check if num1>num2 using ternary operator in python.
  • print num1 if true or num2 otherwise.

Let’s implement the above logic in Python Language.

Python Code

Run
num1, num2 = 20 , 30
print((num1 if num1>num2 else num2))

Output

30

Method 3: Using inbuilt max() Function

In this method we’ll use the inbuilt max() function that take two integers as arguments and returns the largest among them.

Working

Given two input numbers num1 and num2

  • Call inbuilt max() function with num1 and num2 as arguments.
  • Print the returned value.

Let’s implement the above logic in Python Language.

Python Code

Run
num1, num2 = 20, 30
print(max(num1,num2))

Output

30

2 comments on “Find the Greatest of Two Numbers in Python”


  • rohitnamdev876

    n1 = int(input(“Enter the number 1 : “))
    n2 = int(input(“Enter the number 2 : “))
    if n1 < n2:
    print(f"{n2} is Grater")
    # elif n1 == n2:
    # print(f"{n1} and {n2} are equal")
    else:
    print(f"{n1} is grater ")


  • Rubankumar

    a=int(input(“Enter the First number: “))
    b=int(input(“Enter the Last number: “))
    if(a>b):
    print(“The greatest number is “,a)
    elif(b>a):
    print(“The greatest number is “,b)
    else:
    print(“The numbers are equal”)
    In this program there is no case for when numbers are equal here the program for that case too.