How to use boolean 'and' in Python

Python picture Python · Mar 4, 2009 · Viewed 271.8k times · Source

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?

Answer

Mizipzor picture Mizipzor · Mar 4, 2009

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).