A
As in most languages, the difference operator in Python is the !=. 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')
In advance, the operator != implicitly invokes the method ne of the first operating, passing the second as parameter, so if you need to overwrite such operator in a class, it is possible to 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 the overload of the method, the result would be True, indicating that the objects are different, even appearing to be equal.The operator is (or) is not) verifies the identity of objects and not only 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
Lists a and b have the same value, but they are not the same object.Additional readings https://pt.stackoverflow.com/q/38104/5878 https://pt.stackoverflow.com/q/174041/5878 Other existing language operators 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]
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, which returns only the whole part;>>> 5//2
2
And binary, a & b;>>> 1 & 3
1
Exclusive binary OR, a ^ b;>>> 1 ^ 2
3
Binary reversal, ~a;>>> ~2
-3
OR binary, a | b;>>> 1 | 2
3
Exponence, ab;>>> 210
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
Attribution by index, 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]
Binary shift to left, a << b;>>> 4 << 1
8
Binary shift to right, a >> b;>>> 4 >> 1
2
Division rest, a % b;>>> 5 % 2
1
Multiplication, a * b;>>> 2 * 3
6
Multiplication of matrix, a @ b (3.5+);See https://www.python.org/dev/peps/pep-0465/ ;Arithmetic denial, -a;>>> -4
-4
Logical denial, not a;>>> not True
False
Positive, +a;>>> +4
4
Faction, seq[i:j];>>> obj = [1, 2, 3, 4, 5]
>>> obj[1:3]
[2, 3]
Assignment by slice, seq[i:j] = values;>>> obj = [1, 2, 3, 4, 5]
>>> obj[1:3] = [8, 9]
>>> obj
[1, 8, 9, 4, 5]
Exclusion by slicing, del seq[i:j];>>> obj = [1, 2, 3, 4, 5]
>>> del obj[1:3]
>>> obj
[1, 4, 5]
Formatting string, s % obj (prefers method https://pt.stackoverflow.com/q/262184/5878 or https://pt.stackoverflow.com/a/264722/5878 );>>> '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 or equal to, a <= b;>>> 1 <= 2
True
Bigger than, a > b;>>> 1 > 2
False
Greater 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 versions 3+);>>> 1 <> 2
True
Further information can be seen https://docs.python.org/3/library/operator.html .