lundi 20 mai 2019

if decision with list variable

In C/C++, true is standardized as 1, and false as 0. Although not a good practice,

if variable:  
    #do something

kind of decision making seems ok in python and it seems makes decision based on whether variable is None or is zero - if it is a number. Followings are reasonable.

a = 123123
if a:
    print "if condition is true"  # this prints

a = "a string"
if a:
    print "if condition is true"  # this prints

a = None
    if a:
        print "if condition is true"  # this does not print

However, I wonder why it evaluates to false with an empty list, which is neither None, nor zero. What is the exact implementation of python if statement?

a = [] # a is not  None
if a:
   print "true" # this does not print

Aucun commentaire:

Enregistrer un commentaire