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

Join Date
Nov 2012
Posts
6
Rep Power
0

Hi All,

I have a question about Boolean Logical operators. Here is the code and its output.
Code:
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" ?

2. 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
3. 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!?

4. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2007
Location
Joensuu, Finland
Posts
471
Rep Power
70
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.
5. Code:
>>> True == 1
True
>>> False == 0
True
>>> True + 12
13
>>> False * 666
0
>>>
6. 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...
7. 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.
8. 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:

True
False
True
True
False
False
9. 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!
10. 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
''
>>>
11. 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
''
>>>