^= operator in Python












2














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?










share|improve this question






















  • In my experience, the Python docs are absurdly hard to Google. Including "Python language reference" in your search can often lead to better results.
    – JETM
    Nov 19 '18 at 12:55










  • it is xor......
    – mehrdad-pedramfar
    Nov 19 '18 at 12:55






  • 2




    It is not exactly the same as the question I linked, though. ^ is the XOR operator. ^= is the XOR-assignment operator, so a ^= b means "assign a XOR b to a".
    – jdehesa
    Nov 19 '18 at 12:55










  • For some reason, the python docs don't list ^= in the assignment operators reference.
    – OrangeDog
    Nov 19 '18 at 14:03
















2














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?










share|improve this question






















  • In my experience, the Python docs are absurdly hard to Google. Including "Python language reference" in your search can often lead to better results.
    – JETM
    Nov 19 '18 at 12:55










  • it is xor......
    – mehrdad-pedramfar
    Nov 19 '18 at 12:55






  • 2




    It is not exactly the same as the question I linked, though. ^ is the XOR operator. ^= is the XOR-assignment operator, so a ^= b means "assign a XOR b to a".
    – jdehesa
    Nov 19 '18 at 12:55










  • For some reason, the python docs don't list ^= in the assignment operators reference.
    – OrangeDog
    Nov 19 '18 at 14:03














2












2








2







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?










share|improve this question













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?







python






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 '18 at 12:51









sabrinazuraimi

184313




184313












  • In my experience, the Python docs are absurdly hard to Google. Including "Python language reference" in your search can often lead to better results.
    – JETM
    Nov 19 '18 at 12:55










  • it is xor......
    – mehrdad-pedramfar
    Nov 19 '18 at 12:55






  • 2




    It is not exactly the same as the question I linked, though. ^ is the XOR operator. ^= is the XOR-assignment operator, so a ^= b means "assign a XOR b to a".
    – jdehesa
    Nov 19 '18 at 12:55










  • For some reason, the python docs don't list ^= in the assignment operators reference.
    – OrangeDog
    Nov 19 '18 at 14:03


















  • In my experience, the Python docs are absurdly hard to Google. Including "Python language reference" in your search can often lead to better results.
    – JETM
    Nov 19 '18 at 12:55










  • it is xor......
    – mehrdad-pedramfar
    Nov 19 '18 at 12:55






  • 2




    It is not exactly the same as the question I linked, though. ^ is the XOR operator. ^= is the XOR-assignment operator, so a ^= b means "assign a XOR b to a".
    – jdehesa
    Nov 19 '18 at 12:55










  • For some reason, the python docs don't list ^= in the assignment operators reference.
    – OrangeDog
    Nov 19 '18 at 14:03
















In my experience, the Python docs are absurdly hard to Google. Including "Python language reference" in your search can often lead to better results.
– JETM
Nov 19 '18 at 12:55




In my experience, the Python docs are absurdly hard to Google. Including "Python language reference" in your search can often lead to better results.
– JETM
Nov 19 '18 at 12:55












it is xor......
– mehrdad-pedramfar
Nov 19 '18 at 12:55




it is xor......
– mehrdad-pedramfar
Nov 19 '18 at 12:55




2




2




It is not exactly the same as the question I linked, though. ^ is the XOR operator. ^= is the XOR-assignment operator, so a ^= b means "assign a XOR b to a".
– jdehesa
Nov 19 '18 at 12:55




It is not exactly the same as the question I linked, though. ^ is the XOR operator. ^= is the XOR-assignment operator, so a ^= b means "assign a XOR b to a".
– jdehesa
Nov 19 '18 at 12:55












For some reason, the python docs don't list ^= in the assignment operators reference.
– OrangeDog
Nov 19 '18 at 14:03




For some reason, the python docs don't list ^= in the assignment operators reference.
– OrangeDog
Nov 19 '18 at 14:03












4 Answers
4






active

oldest

votes


















1















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.






share|improve this answer























  • What do you mean with "on an object"? Everything in Python is an object, and it may or may not have __ixor__ implemented.
    – timgeb
    Nov 26 '18 at 11:29










  • Ah, okay. I wasn't sure if you were trying to say that numbers are primitive data types like in Java (your top tag).
    – timgeb
    Nov 26 '18 at 11:56



















2














The special method that relates to ^= and is called behind the curtains is __ixor__. The special method that relates to ^ is __xor__. They don't have to behave the same.



__ixor__ is the Augmented Assignment variant of __xor__, performing inplace operations when possible, and falling back to __xor__ when __ixor__ is not implemented.



In principle, the return value of the method is completely up to the implementation of __ixor__, but there are some common use cases.



For two sets, a ^= b computes the symmetric difference of the sets a and b (all elements present in exactly one of the two sets) and mutates a.



>>> a = {1, 2, 3}
>>> b = {1, 4, 5}
>>> reference = a
>>>
>>> a ^= b
>>> a
{2, 3, 4, 5}
>>> reference
{2, 3, 4, 5}


Note that a has been mutated in place, thus a and reference both point to the same set object {2, 3, 4, 5} in memory. Thus ^= behaves differently from ^ in this case, which builds a new set as shown below.



>>> a = {1, 2, 3}
>>> b = {1, 4, 5}
>>> reference = a
>>> a = a^b
>>> a
{2, 3, 4, 5}
>>> reference
{1, 2, 3}


Note that a = a^b has built a new set to which the name a has been reassigned and reference still refers to the original set {1, 2, 3}.



For builtin immutable types (like integers), a ^= b is indeed equivalent to a = a^b. Since integers have no __ixor__ method, Python falls back to using __xor__.
In the case of integers a ^= b performs a bitwise-non-equal operation and the name a is reassigned to the result of that computation.



Demo:



>>> a = 5
>>> b = 6
>>> a^b
3


Explanation:



    101 (5 decimal)
XOR 110 (6 decimal)
-------------------
011 (3 decimal)





share|improve this answer





























    1














    It is XOR. https://docs.python.org/3/reference/expressions.html#binary-bitwise-operations
    For example:



    9 ^ 1


    Output:



    8


    Because in binary 9 is 101, then 1 is 001.
    XOR operation mean:



    101
    001
    output:
    100 #or 8 in natural number





    share|improve this answer





























      1














      a ^= b equialent to a = a^b



      ^ - is XOR operator






      share|improve this answer





















        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375038%2foperator-in-python%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        1















        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.






        share|improve this answer























        • What do you mean with "on an object"? Everything in Python is an object, and it may or may not have __ixor__ implemented.
          – timgeb
          Nov 26 '18 at 11:29










        • Ah, okay. I wasn't sure if you were trying to say that numbers are primitive data types like in Java (your top tag).
          – timgeb
          Nov 26 '18 at 11:56
















        1















        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.






        share|improve this answer























        • What do you mean with "on an object"? Everything in Python is an object, and it may or may not have __ixor__ implemented.
          – timgeb
          Nov 26 '18 at 11:29










        • Ah, okay. I wasn't sure if you were trying to say that numbers are primitive data types like in Java (your top tag).
          – timgeb
          Nov 26 '18 at 11:56














        1












        1








        1







        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.






        share|improve this answer















        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.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 26 '18 at 11:53

























        answered Nov 19 '18 at 12:53









        OrangeDog

        20.7k561135




        20.7k561135












        • What do you mean with "on an object"? Everything in Python is an object, and it may or may not have __ixor__ implemented.
          – timgeb
          Nov 26 '18 at 11:29










        • Ah, okay. I wasn't sure if you were trying to say that numbers are primitive data types like in Java (your top tag).
          – timgeb
          Nov 26 '18 at 11:56


















        • What do you mean with "on an object"? Everything in Python is an object, and it may or may not have __ixor__ implemented.
          – timgeb
          Nov 26 '18 at 11:29










        • Ah, okay. I wasn't sure if you were trying to say that numbers are primitive data types like in Java (your top tag).
          – timgeb
          Nov 26 '18 at 11:56
















        What do you mean with "on an object"? Everything in Python is an object, and it may or may not have __ixor__ implemented.
        – timgeb
        Nov 26 '18 at 11:29




        What do you mean with "on an object"? Everything in Python is an object, and it may or may not have __ixor__ implemented.
        – timgeb
        Nov 26 '18 at 11:29












        Ah, okay. I wasn't sure if you were trying to say that numbers are primitive data types like in Java (your top tag).
        – timgeb
        Nov 26 '18 at 11:56




        Ah, okay. I wasn't sure if you were trying to say that numbers are primitive data types like in Java (your top tag).
        – timgeb
        Nov 26 '18 at 11:56













        2














        The special method that relates to ^= and is called behind the curtains is __ixor__. The special method that relates to ^ is __xor__. They don't have to behave the same.



        __ixor__ is the Augmented Assignment variant of __xor__, performing inplace operations when possible, and falling back to __xor__ when __ixor__ is not implemented.



        In principle, the return value of the method is completely up to the implementation of __ixor__, but there are some common use cases.



        For two sets, a ^= b computes the symmetric difference of the sets a and b (all elements present in exactly one of the two sets) and mutates a.



        >>> a = {1, 2, 3}
        >>> b = {1, 4, 5}
        >>> reference = a
        >>>
        >>> a ^= b
        >>> a
        {2, 3, 4, 5}
        >>> reference
        {2, 3, 4, 5}


        Note that a has been mutated in place, thus a and reference both point to the same set object {2, 3, 4, 5} in memory. Thus ^= behaves differently from ^ in this case, which builds a new set as shown below.



        >>> a = {1, 2, 3}
        >>> b = {1, 4, 5}
        >>> reference = a
        >>> a = a^b
        >>> a
        {2, 3, 4, 5}
        >>> reference
        {1, 2, 3}


        Note that a = a^b has built a new set to which the name a has been reassigned and reference still refers to the original set {1, 2, 3}.



        For builtin immutable types (like integers), a ^= b is indeed equivalent to a = a^b. Since integers have no __ixor__ method, Python falls back to using __xor__.
        In the case of integers a ^= b performs a bitwise-non-equal operation and the name a is reassigned to the result of that computation.



        Demo:



        >>> a = 5
        >>> b = 6
        >>> a^b
        3


        Explanation:



            101 (5 decimal)
        XOR 110 (6 decimal)
        -------------------
        011 (3 decimal)





        share|improve this answer


























          2














          The special method that relates to ^= and is called behind the curtains is __ixor__. The special method that relates to ^ is __xor__. They don't have to behave the same.



          __ixor__ is the Augmented Assignment variant of __xor__, performing inplace operations when possible, and falling back to __xor__ when __ixor__ is not implemented.



          In principle, the return value of the method is completely up to the implementation of __ixor__, but there are some common use cases.



          For two sets, a ^= b computes the symmetric difference of the sets a and b (all elements present in exactly one of the two sets) and mutates a.



          >>> a = {1, 2, 3}
          >>> b = {1, 4, 5}
          >>> reference = a
          >>>
          >>> a ^= b
          >>> a
          {2, 3, 4, 5}
          >>> reference
          {2, 3, 4, 5}


          Note that a has been mutated in place, thus a and reference both point to the same set object {2, 3, 4, 5} in memory. Thus ^= behaves differently from ^ in this case, which builds a new set as shown below.



          >>> a = {1, 2, 3}
          >>> b = {1, 4, 5}
          >>> reference = a
          >>> a = a^b
          >>> a
          {2, 3, 4, 5}
          >>> reference
          {1, 2, 3}


          Note that a = a^b has built a new set to which the name a has been reassigned and reference still refers to the original set {1, 2, 3}.



          For builtin immutable types (like integers), a ^= b is indeed equivalent to a = a^b. Since integers have no __ixor__ method, Python falls back to using __xor__.
          In the case of integers a ^= b performs a bitwise-non-equal operation and the name a is reassigned to the result of that computation.



          Demo:



          >>> a = 5
          >>> b = 6
          >>> a^b
          3


          Explanation:



              101 (5 decimal)
          XOR 110 (6 decimal)
          -------------------
          011 (3 decimal)





          share|improve this answer
























            2












            2








            2






            The special method that relates to ^= and is called behind the curtains is __ixor__. The special method that relates to ^ is __xor__. They don't have to behave the same.



            __ixor__ is the Augmented Assignment variant of __xor__, performing inplace operations when possible, and falling back to __xor__ when __ixor__ is not implemented.



            In principle, the return value of the method is completely up to the implementation of __ixor__, but there are some common use cases.



            For two sets, a ^= b computes the symmetric difference of the sets a and b (all elements present in exactly one of the two sets) and mutates a.



            >>> a = {1, 2, 3}
            >>> b = {1, 4, 5}
            >>> reference = a
            >>>
            >>> a ^= b
            >>> a
            {2, 3, 4, 5}
            >>> reference
            {2, 3, 4, 5}


            Note that a has been mutated in place, thus a and reference both point to the same set object {2, 3, 4, 5} in memory. Thus ^= behaves differently from ^ in this case, which builds a new set as shown below.



            >>> a = {1, 2, 3}
            >>> b = {1, 4, 5}
            >>> reference = a
            >>> a = a^b
            >>> a
            {2, 3, 4, 5}
            >>> reference
            {1, 2, 3}


            Note that a = a^b has built a new set to which the name a has been reassigned and reference still refers to the original set {1, 2, 3}.



            For builtin immutable types (like integers), a ^= b is indeed equivalent to a = a^b. Since integers have no __ixor__ method, Python falls back to using __xor__.
            In the case of integers a ^= b performs a bitwise-non-equal operation and the name a is reassigned to the result of that computation.



            Demo:



            >>> a = 5
            >>> b = 6
            >>> a^b
            3


            Explanation:



                101 (5 decimal)
            XOR 110 (6 decimal)
            -------------------
            011 (3 decimal)





            share|improve this answer












            The special method that relates to ^= and is called behind the curtains is __ixor__. The special method that relates to ^ is __xor__. They don't have to behave the same.



            __ixor__ is the Augmented Assignment variant of __xor__, performing inplace operations when possible, and falling back to __xor__ when __ixor__ is not implemented.



            In principle, the return value of the method is completely up to the implementation of __ixor__, but there are some common use cases.



            For two sets, a ^= b computes the symmetric difference of the sets a and b (all elements present in exactly one of the two sets) and mutates a.



            >>> a = {1, 2, 3}
            >>> b = {1, 4, 5}
            >>> reference = a
            >>>
            >>> a ^= b
            >>> a
            {2, 3, 4, 5}
            >>> reference
            {2, 3, 4, 5}


            Note that a has been mutated in place, thus a and reference both point to the same set object {2, 3, 4, 5} in memory. Thus ^= behaves differently from ^ in this case, which builds a new set as shown below.



            >>> a = {1, 2, 3}
            >>> b = {1, 4, 5}
            >>> reference = a
            >>> a = a^b
            >>> a
            {2, 3, 4, 5}
            >>> reference
            {1, 2, 3}


            Note that a = a^b has built a new set to which the name a has been reassigned and reference still refers to the original set {1, 2, 3}.



            For builtin immutable types (like integers), a ^= b is indeed equivalent to a = a^b. Since integers have no __ixor__ method, Python falls back to using __xor__.
            In the case of integers a ^= b performs a bitwise-non-equal operation and the name a is reassigned to the result of that computation.



            Demo:



            >>> a = 5
            >>> b = 6
            >>> a^b
            3


            Explanation:



                101 (5 decimal)
            XOR 110 (6 decimal)
            -------------------
            011 (3 decimal)






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 19 '18 at 13:42









            timgeb

            49.3k116390




            49.3k116390























                1














                It is XOR. https://docs.python.org/3/reference/expressions.html#binary-bitwise-operations
                For example:



                9 ^ 1


                Output:



                8


                Because in binary 9 is 101, then 1 is 001.
                XOR operation mean:



                101
                001
                output:
                100 #or 8 in natural number





                share|improve this answer


























                  1














                  It is XOR. https://docs.python.org/3/reference/expressions.html#binary-bitwise-operations
                  For example:



                  9 ^ 1


                  Output:



                  8


                  Because in binary 9 is 101, then 1 is 001.
                  XOR operation mean:



                  101
                  001
                  output:
                  100 #or 8 in natural number





                  share|improve this answer
























                    1












                    1








                    1






                    It is XOR. https://docs.python.org/3/reference/expressions.html#binary-bitwise-operations
                    For example:



                    9 ^ 1


                    Output:



                    8


                    Because in binary 9 is 101, then 1 is 001.
                    XOR operation mean:



                    101
                    001
                    output:
                    100 #or 8 in natural number





                    share|improve this answer












                    It is XOR. https://docs.python.org/3/reference/expressions.html#binary-bitwise-operations
                    For example:



                    9 ^ 1


                    Output:



                    8


                    Because in binary 9 is 101, then 1 is 001.
                    XOR operation mean:



                    101
                    001
                    output:
                    100 #or 8 in natural number






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 19 '18 at 12:53









                    Rudolf Morkovskyi

                    720117




                    720117























                        1














                        a ^= b equialent to a = a^b



                        ^ - is XOR operator






                        share|improve this answer


























                          1














                          a ^= b equialent to a = a^b



                          ^ - is XOR operator






                          share|improve this answer
























                            1












                            1








                            1






                            a ^= b equialent to a = a^b



                            ^ - is XOR operator






                            share|improve this answer












                            a ^= b equialent to a = a^b



                            ^ - is XOR operator







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 19 '18 at 12:54









                            Alexander Tolkachev

                            672514




                            672514






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375038%2foperator-in-python%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                MongoDB - Not Authorized To Execute Command

                                How to fix TextFormField cause rebuild widget in Flutter

                                Npm cannot find a required file even through it is in the searched directory