#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    6
    Rep Power
    0

    Question about Boolean


    Hi All,

    I have a question about Boolean Logical operators. Here is the code and its output.
    Code:
    >>> grade = 80 
    >>> grade2 = 90 
    >>> (not grade >= 50) or grade2 >= 50 
    True
    how did that return "True"?

    As I understand.. it fist checked "grade is >=50" which is true and since it has "not" operator, the result will be false.
    And then it evaluates the second expression to check if "grade2 is >= 50" which is true as well and since it has "not" operator, the result will be a false for the second expression.

    So, since they both had a result of false, did it return "True" ?

    Can someone please help me understand its concept?

    Thank you in advance!
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,850
    Rep Power
    481
    See operator precedence

    Executable Iverson notation (j, Ken's final dialect of APL) does not suffer from these precedence rules. Sequences of nouns and verbs evaluate from right to left.

    Anyway, (not grade) evaluates first, yielding False, which compared to 50 converts to integer 0.

    Zero is not greater than or equal to 50 making the term in parentheses False.

    grade2 exceeds 50, which is True.

    False or True
    computes to
    True
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    6
    Rep Power
    0
    Originally Posted by b49P23TIvg
    ......
    Anyway, (not grade) evaluates first, yielding False, which compared to 50 converts to integer 0.

    Zero is not greater than or equal to 50 making the term in parentheses False.

    grade2 exceeds 50, which is True.

    False or True
    computes to
    True
    I'm sorry.. what do you mean by "converts to integer 0" ? and why are you comparing 0 to 50? Only that part did not make sense as my tutor didn't tell me anything about auto-converting!?

    Thank you for your reply, btw.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    434
    Rep Power
    67
    b49P23TIvg already pointed out the precedence, but your logic escapes me here:

    Originally Posted by arvind1212
    then it evaluates the second expression to check if "grade2 is >= 50" which is true as well and since it has "not" operator, the result will be a false for the second expression.
    So you are saying that BECAUSE p is true it is false (where p is grade2 >= 50)?

    Your comparison yields true in all cases where either one of the terms is true. Since grade2 IS >= 50, the whole clause is true whatever precedes it.
    My armada: openSUSE 13.1 (home desktop, home laptop), Crunchbang Linux 11 (mini laptop, work laptop), Ubuntu 14.04 LTS (server), Android 4.2.1 (tablet), Windows 7 Ultimate (testbed)
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,850
    Rep Power
    481
    Code:
    >>> True == 1
    True
    >>> False == 0
    True
    >>> True + 12
    13
    >>> False * 666
    0
    >>>
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    6
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Code:
    >>> True == 1
    True
    >>> False == 0
    True
    >>> True + 12
    13
    >>> False * 666
    0
    >>>
    Thank you sooooo much It does make sense now...
  12. #7
  13. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,850
    Rep Power
    481
    I'm sure you've read this in the manual. Just in case you haven't, the "and" and "or" operators aren't strictly Boolean.
    Code:
    >>> 'cat' and 'dog'  # python needs 'dog'
    'dog'
    >>> 'cat' or 'dog'  # python doesn't evaluate 'dog'
    'cat'
    >>>
    You may eventually realize that "and" and "or" are more useful this way.
    [code]Code tags[/code] are essential for python code and Makefiles!
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    6
    Rep Power
    0
    Originally Posted by SuperOscar
    b49P23TIvg already pointed out the precedence, but your logic escapes me here:



    So you are saying that BECAUSE p is true it is false (where p is grade2 >= 50)?

    Your comparison yields true in all cases where either one of the terms is true. Since grade2 IS >= 50, the whole clause is true whatever precedes it.
    Everything was not true in my case as NOT is being involved. I'm a NOOB and I'm sorry if I have confused you.
    BTW, My doubts have been cleared. I'll go practice more..
    Here is the code output as a FYI..

    Code:
    >>> grade = 80
    >>> grade2 = 90
    
    >>> (not grade >= 50) or grade2 >= 50
    True
    >>> not (grade >= 50 or grade2 >= 50)
    False
    >>> grade >= 50
    True
    >>> grade2 >= 50
    True
    >>> not grade >= 50
    False
    >>> not grade2 >= 50
    False
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    6
    Rep Power
    0
    Originally Posted by b49P23TIvg
    I'm sure you've read this in the manual. Just in case you haven't, the "and" and "or" operators aren't strictly Boolean.
    Code:
    >>> 'cat' and 'dog'  # python needs 'dog'
    'dog'
    >>> 'cat' or 'dog'  # python doesn't evaluate 'dog'
    'cat'
    >>>
    You may eventually realize that "and" and "or" are more useful this way.
    So AND picks the last and OR picks the first? Can you give me the link to that manual? I'll read more about that.

    Code:
    >>> 'cat' and 'dog'
    'dog'
    >>> 'dog' and 'cat'
    'cat'
    
    >>> 'cat' or 'dog'
    'cat'
    >>> 'dog' or 'cat'
    'dog'
    
    >>> 'man' and 'dog' and 'cat'
    'cat'
    >>> 'man' or 'dog' or 'cat'
    'man'
    Thank you!
  18. #10
  19. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,850
    Rep Power
    481
    and or reference

    It's difficult to design good experiments when you're starting.

    >>> 0 or 'ME'
    'ME'
    >>> '' or 'ME'
    'ME'
    >>> 'yes' and False and True
    False
    >>> 'yes' and '' and True
    ''
    >>>
    [code]Code tags[/code] are essential for python code and Makefiles!
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    6
    Rep Power
    0
    Originally Posted by b49P23TIvg
    and or reference

    It's difficult to design good experiments when you're starting.

    >>> 0 or 'ME'
    'ME'
    >>> '' or 'ME'
    'ME'
    >>> 'yes' and False and True
    False
    >>> 'yes' and '' and True
    ''
    >>>
    Interesting. Thanks for the link

IMN logo majestic logo threadwatch logo seochat tools logo