python – if var == False

python – if var == False

Use not

var = False
if not var:
    print learnt stuff

Since Python evaluates also the data type NoneType as False during the check, a more precise answer is:

var = False
if var is False:
    print(learnt stuff)

This prevents potentially unwanted behaviour such as:

var = []  # or None
if not var:
    print(learnt stuff) # is printed what may or may not be wanted

But if you want to check all cases where var will be evaluated to False, then doing it by using logical not keyword is the right thing to do.

python – if var == False

Python uses not instead of ! for negation.

Try

if not var: 
    print learnt stuff

instead

Leave a Reply

Your email address will not be published. Required fields are marked *