Python-Ref > Basic usage > Conditional processing > Non-boolean values in conditions
 
 

<-^^
Klíčová slova
Moduly
Knihovní funkce

Non-boolean values in conditions

What happens if we give if a value that is not boolean.
All data-structures inside Python have their boolean-like value defined, for the use in if and similar statements.
Rather intuitively, zero and empty values are False, while all others are True.
Expand/Shrink
Zdroj: (cond2-1.py)
  1   values = [0,1,2,"0","1","", None, [], [1], [1,2], [0]]
  2   
  3   for v in values:
  4       if v:
  5           print "True", v   # we print "True" for values that evaluate to True
  6       else:
  7           print "False", v  # and False for others
stdout:
False 0
True 1
True 2
True 0
True 1
False 
False None
False []
True [1]
True [1, 2]
True [0]
Doba běhu: 21.0 ms