Python if number equals

Python compare two numbers – Python Program to Check if Two Numbers are Equal Without using Arithmetic and Comparison Operators

Program to Check if Two Numbers are Equal Without using Arithmetic and Comparison Operators

Given two numbers the task is to check whether the given two numbers are equal in Python.

Given First Number = 10 Given Second Number = 10
The given two numbers < 10 , 10 >are Equal
Given First Number = 8 Given Second Number = 15
The given two numbers < 8 , 15 >are Not equal

Program to Check if Two Numbers are Equal Without using Arithmetic and Comparison Operators in Python

Below are the ways to check whether the given two numbers are equal in Python:

Method #1: Using Xor(^) Operator (Static Input)

  • Create a function isEqualNumbers() which takes the given two numbers as arguments and returns true if they are equal else returns false if they are not equal.
  • Inside the isEqualNumbers() function.
  • Apply xor to the first number and second number and store it in a variable say xor_result.
  • Check if the value of xor_result is not equal to 0 using the if conditional statement.
  • If it is true then return False
  • Else return True.
  • Inside the main code.
  • Give the first number as static input and store it in a variable.
  • Give the second number as static input and store it in another variable.
  • Pass the given two numbers as the arguments to isEqualNumbers() function and store the result in a variable Reslt.
  • Check if the Value of Reslt using the If conditional statement.
  • If it is true then print the given two numbers are equal.
  • Else print the given two numbers are not equal.
  • The Exit of the Program.
Читайте также:  Html button style hidden

Below is the implementation:

# Create a function isEqualNumbers() # which takes the given two numbers as arguments and # returns true if they are equal # else returns false if they are not equal. def isEqualNumbers(first_numb, second_numb): # Inside the isEqualNumbers() function. # Apply xor to the first number and second number and # store it in a variable say xor_result. xor_result = first_numb ^ second_numb # Check if the value of xor_result is not equal to 0 # using the if conditional statement. if(xor_result != 0): # If it is true then return False return False # Else return True. return True # Inside the main code. # Give the first number as static input and store it in a variable. firstnumb = 10 # Give the second number as static input and store it in another variable. secondnumb = 10 # Pass the given two numbers as the arguments to isEqualNumbers() function # and store the result in a variable Reslt. Reslt = isEqualNumbers(firstnumb, secondnumb) # Check if the Value of Reslt using the If conditional statement. if(Reslt): # If it is true then print the given two numbers are Equal. print('The given two numbers are Equal') # Else print the given two numbers are Not Equal. else: print('The given two numbers are Not equal')
The given two numbers < 10 , 10 >are Equal

Method #2: Using Xor(^) Operator (User Input)

  • Create a function isEqualNumbers() which takes the given two numbers as arguments and returns true if they are equal else returns false if they are not equal.
  • Inside the isEqualNumbers() function.
  • Apply xor to the first number and second number and store it in a variable say xor_result.
  • Check if the value of xor_result is not equal to 0 using the if conditional statement.
  • If it is true then return False
  • Else return True.
  • Inside the main code.
  • Give the first number as user input using the int(input()) function and store it in a variable.
  • Give the second number as user input using the int(input()) function and store it in another variable.
  • Pass the given two numbers as the arguments to isEqualNumbers() function and store the result in a variable Reslt.
  • Check if the Value of Reslt using the If conditional statement.
  • If it is true then print the given two numbers are equal.
  • Else print the given two numbers are not equal.
  • The Exit of the Program.
Читайте также:  Python посчитать количество файлов

Below is the implementation:

# Create a function isEqualNumbers() # which takes the given two numbers as arguments and # returns true if they are equal # else returns false if they are not equal. def isEqualNumbers(first_numb, second_numb): # Inside the isEqualNumbers() function. # Apply xor to the first number and second number and # store it in a variable say xor_result. xor_result = first_numb ^ second_numb # Check if the value of xor_result is not equal to 0 # using the if conditional statement. if(xor_result != 0): # If it is true then return False return False # Else return True. return True # Inside the main code. # Give the first number as user input using the int(input()) function and store it in a variable. firstnumb = int(input('Enter some random number =')) # Give the second number as user input using the int(input()) function and # store it in another variable. secondnumb = int(input('Enter some random number =')) # Pass the given two numbers as the arguments to isEqualNumbers() function # and store the result in a variable Reslt. Reslt = isEqualNumbers(firstnumb, secondnumb) # Check if the Value of Reslt using the If conditional statement. if(Reslt): # If it is true then print the given two numbers are Equal. print('The given two numbers are Equal') # Else print the given two numbers are Not Equal. else: print('The given two numbers are Not equal')
Enter some random number =8 Enter some random number =15 The given two numbers < 8 , 15 >are Not equal

If you are learning Python then the Python Programming Example is for you and gives you a thorough description of concepts for beginners, experienced programmers.

Источник

Python Program to Check if Two Numbers are Equal Without using Arithmetic and Comparison Operators

Program to Check if Two Numbers are Equal Without using Arithmetic and Comparison Operators

Given two numbers the task is to check whether the given two numbers are equal in Python.

Given First Number = 10 Given Second Number = 10
The given two numbers < 10 , 10 >are Equal
Given First Number = 8 Given Second Number = 15
The given two numbers < 8 , 15 >are Not equal

Program to Check if Two Numbers are Equal Without using Arithmetic and Comparison Operators in Python

Below are the ways to check whether the given two numbers are equal in Python:

Method #1: Using Xor(^) Operator (Static Input)

  • Create a function isEqualNumbers() which takes the given two numbers as arguments and returns true if they are equal else returns false if they are not equal.
  • Inside the isEqualNumbers() function.
  • Apply xor to the first number and second number and store it in a variable say xor_result.
  • Check if the value of xor_result is not equal to 0 using the if conditional statement.
  • If it is true then return False
  • Else return True.
  • Inside the main code.
  • Give the first number as static input and store it in a variable.
  • Give the second number as static input and store it in another variable.
  • Pass the given two numbers as the arguments to isEqualNumbers() function and store the result in a variable Reslt.
  • Check if the Value of Reslt using the If conditional statement.
  • If it is true then print the given two numbers are equal.
  • Else print the given two numbers are not equal.
  • The Exit of the Program.

Below is the implementation:

# Create a function isEqualNumbers() # which takes the given two numbers as arguments and # returns true if they are equal # else returns false if they are not equal. def isEqualNumbers(first_numb, second_numb): # Inside the isEqualNumbers() function. # Apply xor to the first number and second number and # store it in a variable say xor_result. xor_result = first_numb ^ second_numb # Check if the value of xor_result is not equal to 0 # using the if conditional statement. if(xor_result != 0): # If it is true then return False return False # Else return True. return True # Inside the main code. # Give the first number as static input and store it in a variable. firstnumb = 10 # Give the second number as static input and store it in another variable. secondnumb = 10 # Pass the given two numbers as the arguments to isEqualNumbers() function # and store the result in a variable Reslt. Reslt = isEqualNumbers(firstnumb, secondnumb) # Check if the Value of Reslt using the If conditional statement. if(Reslt): # If it is true then print the given two numbers are Equal. print('The given two numbers are Equal') # Else print the given two numbers are Not Equal. else: print('The given two numbers are Not equal')
The given two numbers < 10 , 10 >are Equal

Method #2: Using Xor(^) Operator (User Input)

  • Create a function isEqualNumbers() which takes the given two numbers as arguments and returns true if they are equal else returns false if they are not equal.
  • Inside the isEqualNumbers() function.
  • Apply xor to the first number and second number and store it in a variable say xor_result.
  • Check if the value of xor_result is not equal to 0 using the if conditional statement.
  • If it is true then return False
  • Else return True.
  • Inside the main code.
  • Give the first number as user input using the int(input()) function and store it in a variable.
  • Give the second number as user input using the int(input()) function and store it in another variable.
  • Pass the given two numbers as the arguments to isEqualNumbers() function and store the result in a variable Reslt.
  • Check if the Value of Reslt using the If conditional statement.
  • If it is true then print the given two numbers are equal.
  • Else print the given two numbers are not equal.
  • The Exit of the Program.

Below is the implementation:

# Create a function isEqualNumbers() # which takes the given two numbers as arguments and # returns true if they are equal # else returns false if they are not equal. def isEqualNumbers(first_numb, second_numb): # Inside the isEqualNumbers() function. # Apply xor to the first number and second number and # store it in a variable say xor_result. xor_result = first_numb ^ second_numb # Check if the value of xor_result is not equal to 0 # using the if conditional statement. if(xor_result != 0): # If it is true then return False return False # Else return True. return True # Inside the main code. # Give the first number as user input using the int(input()) function and store it in a variable. firstnumb = int(input('Enter some random number =')) # Give the second number as user input using the int(input()) function and # store it in another variable. secondnumb = int(input('Enter some random number =')) # Pass the given two numbers as the arguments to isEqualNumbers() function # and store the result in a variable Reslt. Reslt = isEqualNumbers(firstnumb, secondnumb) # Check if the Value of Reslt using the If conditional statement. if(Reslt): # If it is true then print the given two numbers are Equal. print('The given two numbers are Equal') # Else print the given two numbers are Not Equal. else: print('The given two numbers are Not equal')
Enter some random number =8 Enter some random number =15 The given two numbers < 8 , 15 >are Not equal

If you are learning Python then the Python Programming Example is for you and gives you a thorough description of concepts for beginners, experienced programmers.

Источник

Python Program to Compare Two Numbers 3 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

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:

Источник

Оцените статью