Which equivalent operator is different in Python?

1

I know some other operators:

  • Greater than: >
  • Less than: <
  • Equality: ==

But what about the "different" operator, as it is in Python?

    
asked by anonymous 25.06.2018 / 22:37

2 answers

18

As with most languages, the difference operator in Python is != . It is worth remembering that it compares only the value between the operands and not their identities.

a = 2
b = 3

if a != b:
    print('a é diferente de b')
else:
    print('a é igual a b')

By now, the != operator implicitly invokes the __ne__ method of the first operand, passing the second as a parameter, so if you need to override that operator in a class, you can do:

class Foo:
    def __init__(self, value):
        self.value = value

    def __ne__(self, other):
        return self.value != other.value

f1 = Foo(1)
f2 = Foo(1)

print(f1 != f2)  # False

Without overloading the method, the result would be True , indicating that the objects are different, even though they appear to be the same.

The is (or is not ) operator checks the identity of objects, not just their values. This is evident, for now, when working with changeable types:

a = [1]
b = [1]

print(a != b)  # False
print(a is not b)  # True

The lists a and b have the same value but are not the same object.

Additional readings

Other operators in the language are:

  • Addition, a + b , when a and b are numeric;

    >>> 1 + 2
    3
    
  • Concatenation, a + b , when a and b are sequences;

    >>> 'Anderson' + ' ' + 'Woss'
    'Anderson Woss'
    >>> [1, 2] + [3, 4]
    [1, 2, 3, 4]
    
  • Contention, a in b ;

    >>> 1 in [1, 2, 3, 4]
    True
    
  • True division, a / b , which returns the actual result;

    >>> 5/2
    2.5
    
  • Division with truncation, a // b , which returns only the integer part;

    >>> 5//2
    2
    
  • And binary, a & b ;

    >>> 1 & 3
    1
    
  • Exclusive binary OR, a ^ b ;

    >>> 1 ^ 2
    3
    
  • Binary inversion, ~a ;

    >>> ~2
    -3
    
  • OR binary, a | b ;

    >>> 1 | 2
    3
    
  • Exponentiation, a**b ;

    >>> 2**10
    1024
    
  • Identity, a is b ;

    >>> 1 is None
    False
    
  • Identity, a is not b ;

    >>> 1 is not None
    True
    
  • Indexing, obj[k] ;

    >>> obj = [1, 2, 3]
    >>> obj[1]
    2
    
  • Index assignment, obj[k] = v ;

    >>> obj = [1, 2, 3]
    >>> obj[2] = 4
    >>> obj
    [1, 2, 4]
    
  • Deletion by index, del obj[k] ;

    >>> obj = [1, 2, 3]
    >>> del obj[1]
    >>> obj
    [1, 3]
    
  • Binary shift to left, a << b ;

    >>> 4 << 1
    8
    
  • Binary shift to right, a >> b ;

    >>> 4 >> 1
    2
    
  • Rest of division, a % b ;

    >>> 5 % 2
    1
    
  • Multiplication, a * b ;

    >>> 2 * 3
    6
    
  • Matrix multiplication, a @ b ( versions 3.5+ );

    See PEP 465 ;

  • Arithmetic negation, -a ;

    >>> -4
    -4
    
  • Logical negation, not a ;

    >>> not True
    False
    
  • Positive, +a ;

    >>> +4
    4
    
  • Slice, seq[i:j] ;

    >>> obj = [1, 2, 3, 4, 5]
    >>> obj[1:3]
    [2, 3]
    
  • Assignment by slicing, seq[i:j] = values ;

    >>> obj = [1, 2, 3, 4, 5]
    >>> obj[1:3] = [8, 9]
    >>> obj
    [1, 8, 9, 4, 5]
    
  • Deletion by slicing, del seq[i:j] ;

    >>> obj = [1, 2, 3, 4, 5]
    >>> del obj[1:3]
    >>> obj
    [1, 4, 5]
    
  • Formatting string , s % obj (prefer method format or f-strings );

    >>> 'Olá, %s' % 'mundo'
    'Olá, mundo'
    
  • Subtraction, a - b ;

    >>> 3 - 1
    2
    
  • Truth test, if obj: ... ;

    >>> obj = 3
    >>> if obj: print('Ok')
    'Ok'
    
  • Less than, a < b ;

    >>> 1 < 2
    True
    
  • Less than or equal to, a <= b ;

    >>> 1 <= 2
    True
    
  • Greater than, a > b ;

    >>> 1 > 2
    False
    
  • Greater than or equal to, a >= b ;

    >>> 1 >= 2
    False
    
  • Enter, not inclusive, a < v < b ;

    >>> v = 5
    >>> 1 < v < 9
    True
    
  • Enter, inclusive, a <= v <= b ;

    >>> v = 5
    >>> 1 <= v <= 9
    True
    
  • Equality, a == b ;

    >>> 1 == 2
    False
    
  • Difference, a != b ;

    >>> 1 != 2
    True
    
  • Difference, a <> b (obsolete from version 2.5, removed in 3+ versions);

    >>> 1 <> 2
    True
    

Further information can be found at official documentation .

    
25.06.2018 / 22:52
-6

In Python you could use different "!=", or not "is not"

if "Foo" != "Bar":
    return "Diferente"

if "Foo" is not "Bar":
    return "Diferente"
    
25.06.2018 / 22:42