Boolean

In [24]:

b = False

if (b):
    print ('It is true')
else:
    print ('It is fake')

It is fake

What is Considered False ?

Everything below are false, anything else are true

In [25]:

print (bool(0))      # zero
print (bool(None))   # none
print (bool(''))     # empty string
print (bool([]))     # empty list
print (bool(()))     # empty tupple
print (bool(False))  # False
print (bool(2-2))    # expression that return any value above
False
False
False
False
False
False
False

and operator

and can return different data types If evaluated result is True, the value of right hand side is returned (because python need to evaluate up to the last value)

In [26]:

print (123 and 2)
print (2 and 123)
print ('aaa' and 'bbb')
print ('bbb' and 'aaa')
print (123 and 'aaa')
print ('aaa' and 123)
2
123
bbb
aaa
aaa
123

or operator

or can return different data type If the first value is true, it will be returned (right hand side value need not be evaluated)

In [27]:

print (0 or 2)
print (0 or 'abcde')
print (1 or 'abcde')
print ('aaaaa' or 'bbbbb')
print (True or 'abc')
2
abcde
1
aaaaa
True

results matching ""

    No results matching ""