Saturday, September 01, 2018

Some Python Logic Operations Tips

Python or and logic Operations Meaning: 

Boolean on functions

All print( ) boolean is false is because print() does not return anything. it returns None. If a function returns a none-zero value, boolean is True

>>> bool(print('aa'))
aa
False
>>> bool(print(''))

False

>>> def add_two(num):
...    return num + 2
...
>>> add_two(3)
5
>>> '' or "" or None or add_two(4) or print('gg')
6
>>> '' or "" or None or add_two(-2) or print('gg')
gg

OR

It  find&return first true or return last expression
examples:
>>> 100 or 33 or 55
100
>>> 100 or 0 or 55

100
>>> '' or "" or None or print('ddd') or print('gg')
ddd
gg
>>> '' or "" or None or print('ddd') or 'aa' or print('gg')
ddd
'aa'
>>> '' or "" or None or print('ddd')
ddd

AND

It find&return first false or return last expression
examples:

>>> 100 and 33 and 55
55
>>> 100 and 0 and 55

0
>>> print('aaa') and print('bbb') and ''

aaa
>>> '' and  "" and None and  print('ddd')
''
>>> 'aa' and '' and print('dddd')
''

No comments: