Python Forum
I'm a bit confused with these boolean operations on integers
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I'm a bit confused with these boolean operations on integers
#1
Hi,

I was doing my python homework recently and I also have a an upcoming practical. In my workbook there are some questions in which an expression is given, we have to first try it out ourselves, then run it in Python and match the output while also providing a reason for the answer. Many questions are of this form:
3 < (10 or 2)
Python evaluates this as True.
3<(2 or 10)
is evaluated as false
What's meant by using a boolean operator on integers? Please help.

Thanks!
Reply
#2
Output:
3 < (10 or 2) must be stated as 3 < 10 or 3 < 2 = True (same as True or False which is True) ______ or _____ True False = True 3 < (2 or 10) must be stated as 3 < 2 or 3 < 10 = True _____ or ______ True True = True
written as you have it:
3 < (10 or 2) if expanded
(3 < 10) or 2 = True

3 < (2 or 10)
(3 < 2) or 10 = False

boolean:
3 < (10 | 2) = True
3 < (2 | 10) = True
Reply
#3
So, basically, it's just expansion of an expression?
Reply
#4
expansion, yes
you can't say:
if a = b or c

need a = b | c

or if not logic:
if a = b or a = c
Reply
#5
With the right tools (functions) you can explain this behavior by yourself.

a = 5 or 7
# is the same like
a = bool(5) or bool(7)
All integers except 0, returns True.
Additional you have to know, that the or operator evaluates the bools until it hits a True.


def state():
    print('Function state called')
    return False

if 42 or state():
    print('If branch')
This example does not call the function state, because 42 was already True.
Truth table of or operation:
Output:
A B Q =========== 0 0 0 1 0 1 0 1 1 1 1 1
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#6
when you perform boolean operation on non-boolean types empty string (''), 0, emprty containers like empty list, empty tuple, etc. are evaluated as false, everything else is evaluated as True.
When you perform or operation, python will stop evaluating expression once it has reached True
When you perform and operation python will stop immediately once it reach False.
When perform and/or Python returns the sub-expression that was evaluated last before evaluating the expression as a whole.
>>> True or []
True
>>> [] and True # note it will not return False, but []
[]
>>> (10 or 2) and False
False
>>> (10 or 2) or  False
10
>>> 10 and 2
2
>>> 2 and 10
10
>>> 10 or 2
10
>>> 2 or 10
2
in your case, because of parenthesis and operator precedence it will first evaluate the expression in the parenthesis on the right-hand side. That is (10 or 2) in the first case and (2 or 10) in the second. In the former case it will be evaluated to True and the result will be the first expression evaluated to True - 10, so
3 < (10 or 2) on first steps is evaluated to
3 < 10 and now this is evaluated to True

in the second case
3 < (2 or 10)
becomes
3 < 2
and is finally evaluated False
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
Documentation >>> 4. Built-in Types >>> 4.1. Truth Value Testing

Quote:Operations and built-in functions that have a Boolean result always return 0 or False for false and 1 or True for true, unless otherwise stated. (Important exception: the Boolean operations or and and always return one of their operands.)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need help implementing an input with file operations Gaijin 3 2,016 Jun-08-2022, 05:50 PM
Last Post: Gaijin
  i'm confused with this if-else assignment gabejohnsonny21 4 1,774 Mar-14-2020, 03:56 PM
Last Post: gabejohnsonny21
  Order of operations for reassignment of a variable StillAnotherDave 1 1,661 Jan-21-2020, 12:20 PM
Last Post: buran
  Arithmetic operations using lists yassine 2 2,353 May-02-2018, 06:20 PM
Last Post: j.crater
  Just a bit confused TimeForged 2 2,538 Feb-09-2018, 11:27 PM
Last Post: TimeForged

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020