4
a = 0
if a == False:
   print a

in php I can say:

$a = 0;
if $a === false {
    echo $a;
}

The triple === in php check for the value within the same type, thus making the integer 0 not be read as a boolean value False How can I do this in python? I would like to differentiate between 0 the integer and False the boolean value in a simple if statement.

2 Answers 2

5

You should use the is keyword in that case. It's the identity operator, the same as === in PHP.

>>> a = 0
>>> if a is False:
...     print a
...
>>> 
Sign up to request clarification or add additional context in comments.

Comments

0

type() will give you the type of an object. But if you're worried about distinguishing between 0 and False then perhaps you should use None instead.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.