How to compare two values in python

FIRST, A CORRECTION TO THE OR CONDITIONAL:

You need to say:

if x == 0 or y == 0 or z == 0:

The reason is that "or" splits up the condition into separate logical parts. The way your original statement was written, those parts were:

x y z == 0 // or 1, 2, 3 depending on the if statement

The last part was fine --- checking to see if z == 0, for instance --- but the first two parts just said essentially if x and if y. Since integers always evaluate to True unless they're 0, that means the first part of your condition was always True when x or y didn't equal 0 (which in the case of y was always, since you had y = 1, causing your whole condition (because of how OR works) to always be True.

To avoid that, you need to make sure all parts of your condition (each side of the OR) make sense on their own (you can do that by pretending that the other side(s) of the OR statement doesn't exist). That's how you can confirm whether or not your OR condition is correctly defined.

You would write the statements individually like so:

if x == 0 if y == 0 if z == 0

which means the correct mergin with the OR keyword would be:

if x == 0 or y == 0 or z == 0

SECOND, HOW TO SOLVE THE PROBLEM:

You're basically wanting to check to see if any of the variables match a given integer and if so, assign it a letter that matches it in a one-to-one mapping. You want to do that for a certain list of integers so that the output is a list of letters. You'd do that like this:

def func(x, y, z): result = [] for integer, letter in zip([0, 1, 2, 3], ['c', 'd', 'e', 'f']): if x == integer or y == integer or z == integer: result.append(letter) return result

Similarly, you could use LIST COMPREHENSION to achieve the same result faster:

def func(x, y, z): return [ letter for integer, letter in zip([0, 1, 2, 3], ['c', 'd', 'e', 'f']) if x == integer or y == integer or z == integer ]

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

How to compare two values in python

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

In the last line we print the result of processing on the screen.

Python Code:

num1=int(input("Number 1 : ")) num2=int(input("Number 1 : ")) if num1>num2: print("Number1 is greater than Number2") elif num1<num2: print("Number1 is less than Number2") else: print("Number1 is equal to Number2")

num1=int(input("Number 1 : "))

num2=int(input("Number 1 : "))

if num1>num2:

    print("Number1 is greater than Number2")

elif num1<num2:

    print("Number1 is less than Number2")

else:

    print("Number1 is equal to Number2")

How to compare two values in python

Many careers in tech pay over $100,000 per year. With help from Career Karma, you can find a training program that meets your needs and will set you up for a long-term, well-paid career in tech.

By continuing you agree to our Terms of Service and Privacy Policy, and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email.


These operators compare the values on either sides of them and decide the relation among them. They are also called Relational operators.

Assume variable a holds 10 and variable b holds 20, then −

Operator Description Example
== If the values of two operands are equal, then the condition becomes true. (a == b) is not true.
!= If values of two operands are not equal, then condition becomes true. (a != b) is true.
<> If values of two operands are not equal, then condition becomes true. (a <> b) is true. This is similar to != operator.
> If the value of left operand is greater than the value of right operand, then condition becomes true. (a > b) is not true.
< If the value of left operand is less than the value of right operand, then condition becomes true. (a < b) is true.
>= If the value of left operand is greater than or equal to the value of right operand, then condition becomes true. (a >= b) is not true.
<= If the value of left operand is less than or equal to the value of right operand, then condition becomes true. (a <= b) is true.

Example

Assume variable a holds 10 and variable b holds 20, then −

#!/usr/bin/python a = 21 b = 10 c = 0 if ( a == b ): print "Line 1 - a is equal to b" else: print "Line 1 - a is not equal to b" if ( a != b ): print "Line 2 - a is not equal to b" else: print "Line 2 - a is equal to b" if ( a <> b ): print "Line 3 - a is not equal to b" else: print "Line 3 - a is equal to b" if ( a < b ): print "Line 4 - a is less than b" else: print "Line 4 - a is not less than b" if ( a > b ): print "Line 5 - a is greater than b" else: print "Line 5 - a is not greater than b" a = 5; b = 20; if ( a <= b ): print "Line 6 - a is either less than or equal to b" else: print "Line 6 - a is neither less than nor equal to b" if ( b >= a ): print "Line 7 - b is either greater than or equal to b" else: print "Line 7 - b is neither greater than nor equal to b"

When you execute the above program it produces the following result −

Line 1 - a is not equal to b Line 2 - a is not equal to b Line 3 - a is not equal to b Line 4 - a is not less than b Line 5 - a is greater than b Line 6 - a is either less than or equal to b Line 7 - b is either greater than or equal to b

python_basic_operators.htm