free stats

Python Program to Find the Large Number

Last Update : 09 Oct, 2022 Python, Programming, Example

 

The best way to learn Python programming is to write programs yourself by practicing examples.

You have given two numbers called number_1 and number_2. The task of this tutorial is to write a program to find a large number from these two numbers.

Program Structure -:

Input: number_1 = 20, number_2 = 44
Output: 44

Input: number_1 = -3, number_2 = -34
Output: -3

Input: number_1 = 3, number_2 = -4
Output: 3

There is more than one method to create this program. These methods are as follows.

 

Method 01 -: 

Find the large number using the max() function. This returns the item with the highest value. 

Syntax of max() function.

max(n1, n2, n3, ...) Or max(iterable)

Example -:

# Python program to find the
# highest value of two numbers
 
number_1 = 20
number_2 = 44
 
highest = max(number_1, number_2)
print(highest)

This program produces the following result -:

44

 

Method 02 -: 

This is the most common approach for comparing numbers. Here, use the if-else statement to find the highest value.

Example -:

# Python program to find the
# highest value of two numbers
 
def highest_val(number_1, number_2):
     
    if number_1 >= number_2:
        return number_1
    else:
        return number_2
     
# Driver code
number_1 = 20
number_2 = 44

highest_value = highest_val(number_1, number_2)
print(highest_value)

This program produces the following result -:

44

 

Method 03 -: 

Find the large number using Ternary Operator. A ternary Operator can be considered a conditional expression which is evaluate something based on true or false conditions. 

Ternary Operator allows you to check a condition in a single line.

# Python program to find the
# highest value of two numbers
    
number_1 = 20
number_2 = 44

# Check with the ternary operator
print(number_1 if number_1 >= number_2 else number_2)

This program produces the following result -:

44

 

You found this tutorial / article valuable? Need to show your appreciation? Here are some options:

01. Spread the word! Use following buttons to share this tutorial / article link on your favorite social media sites.

02. Follow us on Twitter, GitHub ,and Facebook.