As pointed out, "&" in python performs a bitwise and operation, just as it does in C#. and
is the appropriate equivalent to the &&
operator.
Since we're dealing with booleans (i == 5
is True
and ii == 10
is also True
), you may wonder why this didn't either work anyway (True being treated as an integer quantity should still mean True & True is a True value), or throw an exception (eg. by forbidding bitwise operations on boolean types)
The reason is operator precedence. The "and
" operator binds more loosely than ==
, so the expression: "i==5 and ii==10
" is equivalent to: "(i==5) and (ii==10)
"
However, bitwise &
has a higher precedence than "==
" (since you wouldn't want expressions like "a & 0xff == ch
" to mean "a & (0xff == ch)
"), so the expression would actually be interpreted as:
if i == (5 & ii) == 10:
Which is using python's operator chaining to mean: does the valuee of ii anded with 5 equal both i and 10. Obviously this will never be true.
You would actually get (seemingly) the right answer if you had included brackets to force the precedence, so:
if (i==5) & (ii=10)
would cause the statement to be printed. It's the wrong thing to do, however - "&
" has many different semantics to "and
" - (precedence, short-cirtuiting, behaviour with integer arguments etc), so it's fortunate that you caught this here rather than being fooled till it produced less obvious bugs.