Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

^= operator in Python

Tags:

python

I've been using Python a while and I bumped into this operator "^=" for the first time from this link.

def solution(A):
    result = 0
    for number in A:
        result ^= number
    return result

Of course, I did some googling, but I can't seem to find this operator. What does it do?

like image 378
sabrinazuraimi Avatar asked Nov 19 '18 12:11

sabrinazuraimi


People also ask

What are operators in Python?

Operators are special symbols in Python that carry out arithmetic or logical computation. The value that the operator operates on is called the operand. For example: >>> 2+3 5. Here, + is the operator that performs addition. 2 and 3 are the operands and 5 is the output of the operation.

What does >= mean in Python?

The operator >= can be used to compare numeric data types as well as lists and tuples. For a list or tuple, the "greater than or equal to" operator iterates over the lists or tuples and evaluates the elements of both the left and right operands.

What is Itemgetter in Python?

itemgetter operator is a built-in module that contains many operators. itemgetter(n) constructs a function that assumes an iterable object (e.g. list, tuple, set) as input, and fetches the n-th element out of it. — python docs. Example:1 Sorting list of tuples based on the third element in the tuple.


1 Answers

The ^ operator yields the bitwise XOR (exclusive OR) of its arguments, which must be integers.

https://docs.python.org/3/reference/expressions.html#binary-bitwise-operations

As with all the other _= operators, ^= assigns the result back to the variable: a =^ b is eqivalent to a = a ^ b.

As a function it is __ixor__ (or operator.ixor), and may have different behaviour for non-integer types.

like image 150
OrangeDog Avatar answered Nov 13 '22 21:11

OrangeDog