Examples
- Hello World Program in Python (How to Print Hello World in Python?)
- Python Program to Add Two Numbers (Sum/Addition Program)
- How to Generate Random Number in Python? (Program)
- Leap Year Program in Python (Check Leap Year or Not in Python)
- Python Program to Check Number is Positive, Negative, or Zero
- Even Odd Program in Python (How to Check Number is Even or Odd?)
- Python Program to Find Largest of 3 Numbers (Greatest of Three Numbers in Python)
- Prime Number Program in Python (Check Prime or Not)
- Python Program to Find Square Root of a Number (Examples)
- Python Factorial Program (How to Find Factorial of a Number in Python?)
- Python Program to Find Prime Numbers in Range
- Python Program to Find Area of Triangle (Different Ways With Examples)
- How to Solve Quadratic Equation in Python? (Program Examples)
- How to Swap Two Numbers in Python? (Program Examples to Swap Variables)
- Python Program to Convert Kilometers to Miles (km to mi) and Vice Versa
- Python Program to Convert Celsius To Fahrenheit (and Fahrenheit to Celsius)
- Program to Check Armstrong Numbers in Python (Code Examples)
- Program for Armstrong Number Between Two Intervals in Python
- Program to Print Multiplication Table in Python (Code Examples)
- Python Program for Fibonacci Series (Print Fibonacci Sequence in Python)
- Program for Sum of n Natural Numbers in Python (Code Examples)
- Program to Print Powers of 2 in Python (Code Examples)
- Python Program to Find Numbers Divisible by Another Number
- Convert Decimal to Binary, Octal and Hexadecimal in Python (Program with Example)
- How to Convert Decimal to Binary in Python? Program with Examples
- How to Convert Decimal to Octal in Python? Program with Examples
- How to Convert Decimal to Hexadecimal in Python? Program and Examples
- How to Convert Binary to Decimal in Python? Program With Examples
- How to Convert Hexadecimal to Octal in Python? Program With Examples
- Program to Find LCM of Two Numbers in Python (Calculate LCM in Python)
- GCD of Two Numbers in Python (Program to Find HCF or GCD)
- GCD of Three Numbers in Python (HCF Program for n Numbers)
- Simple Calculator Program in Python (Basic Calculator Code)
- Find Factors of a Number in Python (Programs and Examples)
- Program to Find Prime Factors of a Number in Python
- How to Find ASCII Value in Python? ASCII Value of Character
Python Program to Find Largest of 3 Numbers (Greatest of Three Numbers in Python)
In the vast world of programming, one common task is to determine the largest of three numbers in Python. Whether it's analyzing data, solving mathematical problems, or developing algorithms, the ability to identify the greatest number is a fundamental skill every programmer should possess.
So, let’s take a deep dive into the various approaches to finding the greatest of three numbers in Python.
Python offers multiple strategies for solving this problem. We will guide you through each approach, explaining the logic behind the code, and clarifying the output generated by these programs. Regardless of your programming experience, this post aims to make the process of finding the largest of three numbers in Python an enjoyable and educational experience.
By studying these examples, you will gain a deeper understanding of comparison operations, conditional statements, and basic syntax in Python. Additionally, you'll uncover the strengths and weaknesses of each program, allowing you to make informed decisions when choosing the most appropriate solution for your specific needs.
So, whether you are a beginner taking your first steps in Python programming or an experienced developer seeking a refresher, join us on this journey as we explore various ways and easy Python program to find largest of 3 numbers.
Find Greatest of Three Numbers in Python
Code
# Python program to find largest among three numbers
def find_largest(num1, num2, num3):
if num1 >= num2 and num1 >= num3:
largest = num1
elif num2 >= num1 and num2 >= num3:
largest = num2
else:
largest = num3
return largest
# Example usage
number1 = 10
number2 = 25
number3 = 17
largest_number = find_largest(number1, number2, number3)
print("The largest number is:", largest_number)
Output
The largest number is: 25
Explanation
-
We defined a function called find_largest that takes three numbers as input: num1, num2, and num3.
-
We use conditional statements (if, elif, and else) to compare the numbers and determine the largest among them. The function returns the largest number.
-
To test the program, we provide three numbers (number1, number2, and number3) with sample values.
-
We call the find_largest function, passing these numbers as arguments, and store the result in the variable largest_number. Finally, we display the largest number using the print statement.
You can modify the values of number1, number2, and number3 to test the program with different inputs and observe the output.
Maximum of Three Numbers With User Input
Code
# Prompt the user to enter three numbers
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))
# Compare the numbers to find the greatest
if num1 >= num2 and num1 >= num3:
greatest = num1
elif num2 >= num1 and num2 >= num3:
greatest = num2
else:
greatest = num3
# Display the greatest number
print("The greatest number is:", greatest)
Output
Enter the first number: 66
Enter the second number: 33
Enter the third number: 85
The greatest number is: 85.0
Explanation
In this program, we use the input function to prompt the user to enter three numbers. The float function is used to convert the input into floating-point numbers, allowing for decimal values as well.
Next, we use if, elif, and else statements to compare the numbers and determine the greatest among them. The result is stored in the variable greatest.
Finally, we use the print statement to display the greatest number to the user.
When you run this program, it will prompt you to enter three numbers. After you provide the inputs, it will determine the greatest number among them and display the result.
Greatest of Three Numbers in Python Using Function
Code
# Using function to find largest number
def find_greatest(num1, num2, num3):
greatest = max(num1, num2, num3)
return greatest
# Example usage
number1 = 100
number2 = 25
number3 = 127
greatest_number = find_greatest(number1, number2, number3)
print("The greatest number is:", greatest_number)
Output
The greatest number is: 127
Explanation
-
We define a function called find_greatest that takes three numbers as input: num1, num2, and num3.
-
Inside the function, we use the max function to determine the greatest among the three numbers. The max function takes multiple arguments and returns the maximum value.
-
To test the program, we provide three numbers (number1, number2, and number3) with sample values. We call the find_greatest function, passing these numbers as arguments, and store the result in the variable greatest_number. Finally, we display the greatest number using the print statement.
Greatest of Three Numbers in Python Using Nested if
Code
# Python program using nested if to find largest among three numbers
def find_greatest(num1, num2, num3):
if num1 >= num2:
if num1 >= num3:
greatest = num1
else:
greatest = num3
else:
if num2 >= num3:
greatest = num2
else:
greatest = num3
return greatest
# Example usage
number1 = 14
number2 = 203
number3 = 170
greatest_number = find_greatest(number1, number2, number3)
print("The greatest number is:", greatest_number)
Output
The greatest number is: 203
Explanation
-
In this program, we define the find_greatest function that takes three numbers as input: num1, num2, and num3.
-
Inside the function, we use nested if statements to compare the numbers and determine the greatest among them.
-
The outer if statement checks if num1 is greater than or equal to num2. If it is, then we move to the inner if statement. The inner if statement checks if num1 is also greater than or equal to num3. If it is, then num1 is the greatest number. If not, then num3 is the greatest.
-
If the outer if statement evaluates to False, it means that num2 is greater than num1. In this case, we move to the second set of if statements. Here, we check if num2 is greater than or equal to num3. If it is, then num2 is the greatest number. If not, then num3 is the greatest.
- Finally, the function returns the greatest number.
Greatest of Three Numbers in Python Using Ternary Operator
Code
# Python program to check greatest among 3 numbers using ternary operator
def find_greatest(num1, num2, num3):
greatest = num1 if (num1 >= num2 and num1 >= num3) else (num2 if num2 >= num3 else num3)
return greatest
# Example usage
number1 = 10
number2 = 25
number3 = 17
greatest_number = find_greatest(number1, number2, number3)
print("The greatest number is:", greatest_number)
Output
The greatest number is: 25
Explanation
-
We define the find_greatest function that takes three numbers as input: num1, num2, and num3.
-
Inside the function, we use the ternary operator to compare the numbers and determine the greatest among them.
The ternary operator works as follows:
-
num1 is assigned to greatest if the condition (num1 >= num2 and num1 >= num3) is True.
-
If the condition is False, then the second part of the ternary operator (num2 if num2 >= num3 else num3) is evaluated.
-
If num2 is greater than or equal to num3, then num2 is assigned to greatest.
-
If not, then num3 is assigned to greatest.
Finally, the function returns the greatest number.
Practice More Python Programs:
- Hello World Program in Python
- Python Program to Add Two Numbers
- Generate Random Number in Python
- Leap Year Program in Python
- Even Odd Program in Python
- Prime Number Program in Python
- Python Program to Find Square Root of a Number
- Python Factorial Program
- Python Program to Find Area of Triangle
- Solve Quadratic Equation in Python
- Swap Two Numbers in Python
- Python Program to Convert Kilometers to Miles
- Python Program to Convert Celsius To Fahrenheit
- Check Armstrong Numbers in Python
- Print Multiplication Table in Python
- Python Program for Fibonacci Series
- Convert Decimal to Binary, Octal and Hexadecimal in Python
- Find LCM of Two Numbers in Python
- GCD of Two Numbers in Python
- Simple Calculator Program in Python
- Python Matrix Addition Program
- Transpose of Matrix in Python
- Python Matrix Multiplication
- Python Calendar Program