Python Programs | IT Developer
IT Developer

Python Programs



Share with a Friend

Python Programs - Operators and Expressions

Use relational operators between two inputs - Python Program

# Get the first input from the user and convert it to an integer num1 = int(input("Enter the first number: ")) # Get the second input from the user and convert it to an integer num2 = int(input("Enter the second number: ")) # Perform comparisons using relational operators and print the results print(f"Is {num1} equal to {num2}? {num1 == num2}") print(f"Is {num1} not equal to {num2}? {num1 != num2}") print(f"Is {num1} greater than {num2}? {num1 > num2}") print(f"Is {num1} less than {num2}? {num1 < num2}") print(f"Is {num1} greater than or equal to {num2}? {num1 >= num2}") print(f"Is {num1} less than or equal to {num2}? {num1 <= num2}")

Output

 
OUTPUT  :
Enter the first number: 10
Enter the second number: 5
Is 10 equal to 5? False
Is 10 not equal to 5? True
Is 10 greater than 5? True
Is 10 less than 5? False
Is 10 greater than or equal to 5? True
Is 10 less than or equal to 5? False 

Explanation

  • Input Collection:

The program begins by prompting the user to enter two numbers using the input() function. The entered values, which are initially strings, are converted to integers using int() to allow for numerical comparisons.

  • Relational Operators:

Python's relational operators are then used to compare num1 and num2:

  • ==(Equal to): Checks if the values are identical.
  • !=(Not equal to): Checks if the values are different.
  • >(Greater than): Checks if the first value is strictly larger than the second.
  • <(Less than): Checks if the first value is strictly smaller than the second.
  • >=(Greater than or equal to): Checks if the first value is larger than or equal to the second.
  • <=(Less than or equal to): Checks if the first value is smaller than or equal to the second.
  • Output:

The print() function displays the result of each comparison, which will be either True or False, indicating whether the condition is met. The f-string formatting is used for clear and readable output.