Booleans Values

55 Views Asked by At

Which of the following will be fully evaluated that first option will be checked inclusive the second option, if the following variables represent the following boolean values

a = True
b = False 

not a or b
a and b
a or b
not and b
b and a
b or a

I tried applying the booleans principles of and, or, not seems I still can't get it correct am failing. I evaluated the options as follows

not a or b **short circuit** 

a and b **full evaluation** 

a or b **short circuit**

not a and b **full evaluation**

b and a **short circuit**

b or a **full evaluation**
1

There are 1 best solutions below

0
furas On

Instead of variables a = True b = False you could use functions with print() to see which function was executed.

def a():
    print('a = True')
    return True
    
def b():
    print('b = False')
    return False

print('---')
print('not a or b')
print('result:', (not a()) or b())

print('---')
print('a and b')
print('result:', a() and b() )

print('---')
print('a or b')
print('result:', a() or b())

print('---')
print('not a and b')
print('result:', (not a()) and b())

print('---')
print('b and a')
print('result:', b() and a())

print('---')
print('b or a')
print('result:', b() or a())

Result:

--
not a or b
a = True
b = False
result: False
---
a and b
a = True
b = False
result: False
---
a or b
a = True
result: True
---
not a and b
a = True
result: False
---
b and a
b = False
result: False
---
b or a
b = False
a = True
result: True