Last Updated: February 25, 2016
·
801
· karan_ta

python has xor operator as !=

example
a = 1
b = 0
c = a != b

print c

output is True

2 Responses
Add your response

More generally: bool(a) != bool(b). Then the types of a and b can even be different.

over 1 year ago ·

Python does not have xor operator implemented as !=:

>>> code = compile('a = 1; b = 0; a != b;', '<STDIN>', 'single')
>>> dis(code)
  1           0 LOAD_CONST               0 (1)
              3 STORE_NAME               0 (a)
              6 LOAD_CONST               1 (0)
              9 STORE_NAME               1 (b)
             12 LOAD_NAME                0 (a)
             15 LOAD_NAME                1 (b)
             18 COMPARE_OP               3 (!=)
             21 PRINT_EXPR          
             22 LOAD_CONST               2 (None)
             25 RETURN_VALUE        
>>> code = compile('a = 1; b = 0; a ^ b;', '<STDIN>', 'single')
>>> dis(code)
  1           0 LOAD_CONST               0 (1)
              3 STORE_NAME               0 (a)
              6 LOAD_CONST               1 (0)
              9 STORE_NAME               1 (b)
             12 LOAD_NAME                0 (a)
             15 LOAD_NAME                1 (b)
             18 BINARY_XOR          
             19 PRINT_EXPR          
             20 LOAD_CONST               2 (None)
             23 RETURN_VALUE        

As you can see, these both generate different bytecode (the first calls into longrichcompare and the second calls into longxor in longobject.c).

over 1 year ago ·