Python Program to Find the Largest Number Among Three Numbers

In this python tutorial, you will learn how to Find the Largest Number Among Three Numbers using the input function, print function, if, elif, else statements and greater than – equal to (>=) operator of the python programming language.

How to Find the Largest Number Among Three Numbers?

Let’s take a look at the source code , here the values are given as input by the user in the code, the >= greater than – equal to operator along with the if, elif and else statements carry out the function.

RUN CODE SNIPPET
# Python program to find the largest number among the three input numbers

#three numbers are entered by the user
num1 = float(input("Enter first number: "))
num2 = float(input("\nEnter second number: "))
num3 = float(input("\nEnter third number: "))
 
if (num1 >= num2) and (num1 >= num3):
   largest = num1
elif (num2 >= num1) and (num2 >= num3):
   largest = num2
else:
   largest = num3
 
print("\nThe largest number is",largest)

INPUT:

12
87
43

OUTPUT:

Enter first number: 
Enter second number: 
Enter third number: 
The largest number is 87.0
  1. Here we give the user the option to enter the values and the input values are scanned using the input function and  the variables are stored num with the string/statements "Enter first number: ", "Enter second number: ", "Enter third number: ".
  2. The float function is used to convert a number stored in a string or integer into a floating point number, in other words, it is a number with a decimal point.
  3. In the STDIN section of the code editor the input values are entered.
  4. In the next line of the code, we declare the if with the conditions (num1 >= num2) and (num1 >= num3) followed by a colon : , if the conditions are met, the num1 value is returned. The values are stored in the variable largest.
  5. We declare the elif with the conditions (num2 >= num1) and (num2 >= num3), if the conditions are met followed by a colon :, the num2 value is returned. The values are stored in the variable largest.
  6. If the above conditions are not met, the else statement is executed and num3 value is returned. The values are stored in the variable largest.
  7. The print function displays the statement "\nThe largest number is"  with the declared variable largest number after the comma which holds the returned value.

NOTE:

  • The greater than – equal to (>=) operator is a comparison operator, it is used to compare values. It returns either True or False according to the condition.
  • The input() function allows a user to insert a value into a program, it returns a string value.
  • The if and else statements evaluates whether an expression is true or false. If a condition is true, the “if” statement is executed otherwise, the “else” statement is executed.
  • The elif is short for else if, it allows us to check for multiple expressions.
  • The colon : at the end of the if, elif and else statement tells Python that the next line of code should only be run if the condition is true.
  • The statement for the input function are enclosed in single quotes and parenthesis.
  • The \n in the code indicates a new line or the end of a statement line or a string.
  • The print statement/string to be displayed in enclosed in double quotes.

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

In this python tutorial, you will learn how to Display Prime Numbers Between Two Intervals using the if and else...
In this python tutorial, you will learn how to Calculate Standard Deviation with built in functions of the python programming...
In this Python program, we will convert temperature values from Celsius to Fahrenheit. The Celsius and Fahrenheit scales are two...