Friday, January 31, 2020

True and False values

True and False values in Python


All the values in Python are True except (All Boolean expressions return either True or False)
  1. None
  2. False
  3. Number values equal to 0 like 0, 0,0, 0L and 0j 
  4. All empty sequences like ' ', "",(),[] and mappings like {}
  5. All user-defined types having __bool__ and __len__ method return values as 0 or False.
None is like undef or Null or Nill in other languages. To test all of the above we can use not operator in Python, which returns the reverse of any Boolean expression on which it is applied.

#Booleans in Python
if not(False):
 
print(True) #True
if not(None):
 
print(True#True
if not(0):  #False is 0 and True is 1
 
print(True#True
if not(0.0):
 
print(True#True
if not([]):   
 
print(True#True
if not(()):
 
print(True)   #True
if not([]):
 
print(True)   #True
if not({}):
 
print(True)   #True
if not(''):
 
print(True)   #True
if not(""):
 
print(True)   #True

Try yourself: