Python Program to Find the Largest Among Three Numbers

Problem Definition

Create a Python program to find the largest among the three given numbers.

In this article, we will show you two methods to address this same problem one being the conventional way and the other being the Pythonic way.

Find The Largest Number Using Conditional Statements

  1. Take inputs from the user and store them in variables
  2. Compare the numbers
  3. Print the final result
  4. End

To understand the program you need to have an understanding of following Python concept.

num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
num3 = float(input("Enter third number: "))
 
if (num1 > num2) and (num1 > num3):
   largest = num1
elif (num2 > num1) and (num2 > num3):
   largest = num2
else:
   largest = num3
 
print("The largest number is",largest)

Runtime Test Cases

Enter first number: 1
Enter second number: 2
Enter third number: 3
The largest number is 3.0

Find The Largest Number Using List Function

num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
num3 = float(input("Enter third number: "))

my_list = [num1, num2, num3]
Largest = max(my_list)
print("The Largest number is", Largest)

Runtime Test Cases

Enter first number: 1
Enter second number: 2
Enter third number: 3
The largest number is 3.0

In this approach, we are making a list of all the inputs and using the max() method which returns the item with the highest value in the list.

Support Django Central

If you appreciate my work, or if it has helped you along your journey. It would mean a lot to me if you could write a message on my wall and share a cup of coffee (or tea) with me.
Buy Me a Coffee at ko-fi.com

2 thoughts on “Python Program to Find the Largest Among Three Numbers”

  1. Write a Python program to input 3 numbers and find the largest. Print all the numbers, and the largest among them, with appropriate titles. Display appropriate message and exit from the program if any of these inputs is not a numeric value.

    try :
    n1=float(input(“Enter number 1: “))
    n2=float(input(“Enter number 2: “))
    n3=float(input(“Enter number 3: “))
    if n1 > n2 :
    if n1 > n3 :
    LNo=n1
    else :
    LNo=n3
    else :
    if n2 >n3 :
    LNo=n2
    else :
    LNo=n3
    print(“DISPLAY……..”)
    print(“Number 1 : “,n1)
    print(“Number 2 : “,n2)
    print(“Number 3 : “,n3)
    print(“Largest number is : “,LNo)
    except :
    print(“invalid input…..”)

    Reply
  2. n1=float(input(“Enter number 1: “))
    n2=float(input(“Enter number 2: “))
    n3=float(input(“Enter number 3: “))
    if n1 > n2 :
    if n1 > n3 :
    LNo=n1
    else :
    LNo=n3
    else :
    if n2 >n3 :
    LNo=n2
    else :
    LNo=n3
    print(“DISPLAY……..”)
    print(“Number 1 : “,n1)
    print(“Number 2 : “,n2)
    print(“Number 3 : “,n3)
    print(“Largest number is : “,LNo)

    Reply

Leave a Comment