[Solved] Choosing the right XOR in Python: | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

[Solved] Choosing the right XOR in Python:

Which would you use in this code? The long one that I commented out? The one I used(rather an equivalent of xor) ? The bitwise one that I also commented out? I did this simple exercise (from a website) which I realised used XOR as I was coding it. I haven't advanced in Python Core as far as the Pythonicness module, since I am trying to find and do exercises about previous topics, and this exercise was in that site's conditionals section, and as I said I bumped into XOR while I was doing it. Was looking to find the syntax to shorten it, and there were StackOverflow pages talking about XOR and Pythonicness, which led me to ask the question. The code is added and you can see the exercise statement (paraphrased). If you think I should just go through my SoloLearn modules, please let me know about that, too. Thank you. https://code.sololearn.com/c0QrjWYnmQ8u/?ref=app

4th Apr 2022, 11:48 AM
Korkunç el Gato
Korkunç el Gato - avatar
6 Answers
+ 6
Choose the one you find the most readable. You might be interested in the built-in xor operator too: from operator import xor print(xor(1,0))
4th Apr 2022, 1:31 PM
Lisa
Lisa - avatar
+ 3
Another thought: if you are going to apply the xor on the same type of data repeatedly, you could write your own xor function... HungryTradie It's #7ea, the rabbit had to sit down and calm a bit.
5th Apr 2022, 7:01 AM
Lisa
Lisa - avatar
+ 2
On the same SO page I saw logical_xor() which didn't work with my code. At first I searched "is xor a keyword in python", which returned said pages, and I saw "^" and "!=" operators there along with the logical_xor(), And Lisa's reply made me remember reading about how built-in functions weren't keywords. So, ofc, my search was fruitless in that department. This is feedback for newbies like me. Thanks to Lisa for the informative response. So there is actually a short and non bitwise xor. Thank you.
4th Apr 2022, 1:50 PM
Korkunç el Gato
Korkunç el Gato - avatar
+ 1
Hey Lisa are you feeling ok, you look a little green around the edges.....
5th Apr 2022, 6:44 AM
HungryTradie
HungryTradie - avatar
+ 1
Isn't Korkunç TheTerrible a true champion, updates to [Solved] and leaves clues for those that search the forum 🥇 I wonder if there is something like catnip for rabbits.... or maybe rabbit meditation to become a zen bunny.
5th Apr 2022, 7:04 AM
HungryTradie
HungryTradie - avatar
+ 1
Thank you :-) Catnip equivalent for coding bunnies? That has to be hard to find. Look at all her 90's pixel edges. Those alone could scrape away a carrot into oblivion :D
5th Apr 2022, 7:34 AM
Korkunç el Gato
Korkunç el Gato - avatar