Write a program to take two integer value from the user and check both the values are same in python

I am new to python and I do not know how to go about this problem exactly. If I want to compare two integers to see if they are equal, how would I set that up? For example, enter a number for a, enter a number for b and see if they are equal or not?

user1590499

9013 gold badges10 silver badges17 bronze badges

asked Sep 15, 2012 at 1:25

a = input("Enter the first number: ")
b = input("Enter the second number: ")
# if a is b: - Compares id's, and can cause inconsistencies. Use == instead.
if a == b:
  print "Both inputs are equal"
else:
  print "Your input is not equal."

answered Sep 15, 2012 at 1:32

AestheteAesthete

18.2k6 gold badges34 silver badges45 bronze badges

6

a=20
b=10
if a==b:
 print "equal"
else:
 print "unequal"

answered Sep 15, 2012 at 1:26

user1655481user1655481

3861 silver badge10 bronze badges

def compare(a, b):
   return a == b

You could write a function like this and call it, like so:

aNumber = 3
anotherNumber = 4
result = compare(aNumber, anotherNumber)
print(result)

answered Sep 15, 2012 at 1:27

BorgleaderBorgleader

15.6k5 gold badges43 silver badges61 bronze badges

6

Last update on August 19 2022 21:50:48 (UTC/GMT +8 hours)

Python Basic: Exercise-134 with Solution

Write a Python program to input two integers in a single line.

Sample Solution-1:

Python Code:

print("Input the value of x & y")
x, y = map(int, input().split())
print("The value of x & y are: ",x,y)

Sample Output:

Input the value of x & y
 2 4
The value of x & y are:  2 4

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Sample Solution-2:

Python Code:

a, b = [int(a) for a in input("Input the value of a & b: ").split()]
print("The value of a & b are:",a,b)

Sample Output:

Input the value of a & b:  2 4
The value of a & b are: 2 4

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to calculate the time runs (difference between start and current time)of a program.
Next: Write a Python program to print a variable without spaces between values.

Last update on August 19 2022 21:50:47 (UTC/GMT +8 hours)

Python Basic: Exercise-35 with Solution

Write a Python program which will return true if the two given integer values are equal or their sum or difference is 5.

Pictorial Presentation:

Write a program to take two integer value from the user and check both the values are same in python

Sample Solution:

Python Code:

def test_number5(x, y):
   if x == y or abs(x-y) == 5 or (x+y) == 5:
       return True
   else:
       return False
print(test_number5(7, 2))
print(test_number5(3, 2))
print(test_number5(2, 2))
print(test_number5(7, 3))
print(test_number5(27, 53))

Sample Output:

True
True
True
False
False

Flowchart:

Write a program to take two integer value from the user and check both the values are same in python

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to sum of two given integers. However, if the sum is between 15 to 20 it will return 20.
Next: Write a Python program to add two objects if both objects are an integer type.

How do you check if two integers are the same in Python?

If equals test in Python: if with == The equals ( == ) operator tests for equality. It returns True when both tested values are the same. When their values differ, the operator returns False .

How do you check if a number is between two values in Python?

Use the comparison operators to check if a number is between two numbers. Use the syntax minimum <= number <= maximum such that maximum is greater than minimum to return a boolean indicating whether number falls between minimum and maximum on a number line.

How do you compare 2 numbers in Python?

Both “is” and “==” are used for object comparison in Python. The operator “==” compares values of two objects, while “is” checks if two objects are same (In other words two references to same object).

How do you get two integer inputs in the same line in Python?

One solution is to use raw_input() two times. Note that we don't have to explicitly specify split(' ') because split() uses any whitespace characters as a delimiter as default.