Programming Code Examples

Python Program to Compare Two Numbers3 min read

In this  Python Example, I’ll show how to compare two numbers using if else if statements.

We use the following operators for comparison in Python. In this example we will perform a simple comparison process.

Compare Two Numbers




Variables are defined in the first row. In the next lines, values are assigned to these variables. Then  Numbers compare in if statement.

How to Compare Two Numbers

Comparing two numbers involves determining which number is larger or smaller, or whether the numbers are equal. This can be done using a variety of techniques and programming languages.

One way to compare two numbers is to use an if-else statement or a similar control flow structure. For example, in Python you can use an if-elif-else statement like this:

In this example, the program checks if num1 is greater than num2. If it is, it executes the code in the first block. If num1 is not greater than num2, the program checks if num1 is smaller than num2. If it is, it executes the code in the second block. If num1 is not smaller than num2, the program executes the code in the third block.

Another way to compare two numbers is to use a comparison operator such as > (greater than), < (less than), >= (greater than or equal to), or <= (less than or equal to). These operators return a Boolean value (True or False) indicating whether the comparison is true or false.

Python Program to Compare Two Numbers

For example, in Python you can use a comparison operator like this. In the last line we print the result of processing on the screen.

Python Code:

This program first prompts the user to enter two numbers, which are stored in the variables num1 and num2. It then uses an if-elif-else statement to compare the numbers and output a message indicating which is larger.

If num1 is greater than num2, the program outputs a message saying that num1 is larger. If num1 is less than num2, the program outputs a message saying that num1 is smaller. If num1 is equal to num2, the program outputs a message saying that the two numbers are equal.

Output:

2 comments

  • #Take two numbers from the user to compare
    num1 = int(input(“50:”))
    num2 = int(input(“70:”))
    # Compare both the number
    if num1 num2):
    print(“50 > 70.”)
    else (num1 50.”)
    Literally! can’t find the mistake Help me please!

  • is there any another way ?
    i intested in to find Another Solution about this …
    Thank you so much for your Help