Which operator is equivalent to different in Python?

Asked

Viewed 52,286 times

1

I know some other operators:

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

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

  • 2

    https://stackoverflow.com/a/11060508/4312593

  • 1

    I got it sorted, thank you!

  • 1

    @Leocaracciolo only in versions 2+, but was removed in versions 3+

  • @Andersoncarloswoss, good to know!! Updated me

2 answers

23

As with most languages, the difference operator in Python is !=. Remember 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')

In advance, the operator != implicitly invokes the method __ne__ of the first operand, passing the second as parameter, so if you need to override such 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 operator is (or is not) checks the identity of objects and not only their values. This is evident, for now, when worked 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 existing operators in the language are:

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

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

    >>> 'Anderson' + ' ' + 'Woss'
    'Anderson Woss'
    >>> [1, 2] + [3, 4]
    [1, 2, 3, 4]
    
  • Containment, 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, that returns only the entire part;

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

    >>> 1 & 3
    1
    
  • OR binary exclusive, 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]
    
  • Exclusion by index, del obj[k];

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

    >>> 4 << 1
    8
    
  • Right torque displacement, a >> b;

    >>> 4 >> 1
    2
    
  • Division rest, 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 denial, not a;

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

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

    >>> obj = [1, 2, 3, 4, 5]
    >>> obj[1:3]
    [2, 3]
    
  • Slice assignment, seq[i:j] = values;

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

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

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

    >>> 3 - 1
    2
    
  • Test of truth, 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 including, a < v < b;

    >>> v = 5
    >>> 1 < v < 9
    True
    
  • Between, 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 as of version 2.5, removed in versions 3+);

    >>> 1 <> 2
    True
    

Further information can be found on official documentation.

-6

In Python you could use Different "!=" or Not "is not"

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

if "Foo" is not "Bar":
    return "Diferente"
  • 7

    != and is not are completely different things.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.