51

In C# we can use && (boolean and) like this:

int i = 5;
int ii = 10;
if(i == 5 && ii == 10) {
    Console.WriteLine("i is 5, and ii is 10");
}
Console.ReadKey(true);

But try that with python:

i = 5
ii = 10
if i == 5 && ii == 10:
    print "i is 5 and ii is 10";      

I get an error: SyntaxError: invalid syntax

If I use a single &, at least I get no syntax error. How do I do a boolean && in Python?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335

6 Answers6

76

Try this:

i = 5
ii = 10
if i == 5 and ii == 10:
      print "i is 5 and ii is 10"

Edit: Oh, and you dont need that semicolon on the last line (edit to remove it from my code).

Mizipzor
  • 51,151
  • 22
  • 97
  • 138
  • The semicolon is useful if we want to combine lines of codes. – riza Mar 04 '09 at 13:24
  • 1
    Combining lines is not the "pythonic" way though – Rodrigo Mar 04 '09 at 20:45
  • 1
    Although you can combine lines, as Rodrigo says, its not "pythonic". Since in this particular case the semicolon had no effect at all I made a comment about its redundancy. I assumed the questioner was new to python and didnt want to bloat my answer as to what is "pythonic" and what semicolon does. – Mizipzor Mar 05 '09 at 09:06
29

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.

Brian
  • 116,865
  • 28
  • 107
  • 112
16

The correct operator to be used are the keywords 'or' and 'and', which in your example, the correct way to express this would be:

if i == 5 and ii == 10:
    print "i is 5 and ii is 10"

You can refer the details in the "Boolean Operations" section in the language reference.

Seh Hui Leong
  • 1,112
  • 1
  • 8
  • 19
7

You can also test them as a couple.

if (i,ii)==(5,10):
    print "i is 5 and ii is 10"
fulmicoton
  • 15,502
  • 9
  • 54
  • 74
6

& is used for bit-wise comparison. use and instead. and btw, you don't need semicolon at the end of print statement.

SilentGhost
  • 307,395
  • 66
  • 306
  • 293
6

In python, use and instead of && like this:

#!/usr/bin/python
foo = True;
bar = True;
if foo and bar:
    print "both are true";

This prints:

both are true
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Tim Ottinger
  • 1,442
  • 9
  • 5