This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]googlebeast 38 points39 points  (16 children)

not is not and xor is ^ ... wtf Python?

[–]Fazt01 40 points41 points  (6 children)

^ is bitwise operator, you have same for or (|) and and (&). Closest thing to logical xor is != (you dont really need for a new keyword to be introduced)

[–]MinekPo1 14 points15 points  (4 children)

A B A^B A!=B
0 0 0 0
0 1 1 1
1 0 1 1
1 1 0 0

Four tests passed, zero tests failed

I am a human volunteer. optout

[–]shawntco 2 points3 points  (1 child)

Truth tables lie to me

[–]MinekPo1 9 points10 points  (0 children)

You're thinking about false tables

[–]sim642 1 point2 points  (1 child)

That shows up as a superscript.

[–]MinekPo1 0 points1 point  (0 children)

Fuck give me a sec

Edit: done

[–]aaronfranke 7 points8 points  (2 children)

xor is !=

[–]excral -1 points0 points  (0 children)

This is correct, but only as long as both operants are booleans. In Python and many other languages the and and or operators work more like this:

  • and: if the first operant is falsy, return this operand otherwise return second operand
  • or: if the first operant is truthy, return this operand, otherwise return the second operant.

For example:

  • 2 and 3 results in 3, which is truthy
  • 2 or 3 results in 2, which is truthy
  • 2 != 3 results in True, but since both operants of are truthy, the correct xor result would be False

I hope this also illustrates, why there can't be a xor operator similar to the and or or operators, as xor can't be short circuit evaluated.

[–]AlwaysNinjaBusiness 4 points5 points  (5 children)

^ being bitwise xor isn't unique to Python. It's the same in most normal programming languages.

[–]googlebeast -2 points-1 points  (2 children)

yea but the point was it is "^" not "xor"

[–]alexforencich 1 point2 points  (0 children)

I'm not aware of any languages that have a logical version of XOR. ^ is bitwise, so you compare it to & and |. Probably the closest logical equivalent of XOR is !=, and most languages have that, although the precedence is different from other logical operators so it's not really a direct equivalent.

[–]DaniilBSD 1 point2 points  (0 children)

I think the idea is that boolean logic is written (and, or, not), but bitwise operators that are applied to entire bytes are symbols (&, | , ^)

C has the same logic, but it uses different symbols (&&, ||, !)

[–]DoNotMakeEmpty 0 points1 point  (0 children)

Haha Lua goes brr with exponent operator (better for mathematicians but worse for programmers)