Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Boolean Operator Confusion

53 views
Skip to first unread message

subhabrat...@gmail.com

unread,
Apr 24, 2015, 10:50:24 AM4/24/15
to
Dear Group,

I am trying to understand the use of Boolean operator in Python. I am trying to
write small piece of script, as follows,

def input_test():
str1=raw_input("PRINT QUERY:")
if "AND" or "OR" or "NOT" in str1:
print "It is a Boolean Query"
elif "AND" or "OR" or "NOT" not in str1:
print "It is not a Boolean Query"
else:
print "None"

I am trying to achieve if the words "AND","OR","NOT" ... in string I want to recognize it as,
Boolean Query, if they are not there the string is not Boolean query.
As I am trying to test it I am getting following output,

>>> input_test()
PRINT QUERY:obama AND clinton
It is a Boolean Query
>>> input_test()
PRINT QUERY:obama clinton
It is a Boolean Query

To understand Boolean operators I have tried to read the follwing link,
https://docs.python.org/release/2.5.2/lib/boolean.html

and practised script like the following:
>>> def currency(n):
x1=raw_input("PRINT Your Country")
x2=raw_input("Print Your Currency")
if x1=="USA" and x2=="Dollar":
print "Correct"
elif x1=="USA" and x2!="Dollar":
print "Not Matching"
else:
print "None"


>>> currency(1)
PRINT Your CountryUSA
Print Your CurrencyDollar
Correct
>>>

But I am doing something wrong. If anybody may kindly suggest.

Regards,
Subhabrata Banerjee.







Ian Kelly

unread,
Apr 24, 2015, 11:01:07 AM4/24/15
to Python
On Fri, Apr 24, 2015 at 8:50 AM, <subhabrat...@gmail.com> wrote:
> Dear Group,
>
> I am trying to understand the use of Boolean operator in Python. I am trying to
> write small piece of script, as follows,
>
> def input_test():
> str1=raw_input("PRINT QUERY:")
> if "AND" or "OR" or "NOT" in str1:

This is equivalent to:

if ("AND") or ("OR") or ("NOT" in str1):

Which is always true because the truth value of the literal strings
"AND" and "OR" are true.

It is not equivalent to:

if ("AND" in str1) or ("OR" in str1) or ("NOT" in str1):

Steven D'Aprano

unread,
Apr 24, 2015, 11:15:04 AM4/24/15
to
On Sat, 25 Apr 2015 12:50 am, subhabrat...@gmail.com wrote:

> Dear Group,
>
> I am trying to understand the use of Boolean operator in Python. I am
> trying to write small piece of script, as follows,
>
> def input_test():
> str1=raw_input("PRINT QUERY:")
> if "AND" or "OR" or "NOT" in str1:
> print "It is a Boolean Query"
> elif "AND" or "OR" or "NOT" not in str1:
> print "It is not a Boolean Query"
> else:
> print "None"

First problem: why do you sometimes return "None"? You have two possible
answers: either something is a boolean query, or it is not. There is no
third choice. ("It's a boolean query, but only on Wednesdays.")

So your code you have if...else and no "elif" needed.

if <test for boolean query>:
print "It is a Boolean Query"
else:
print "It is not a Boolean Query"


Now let us look at your test for a boolean query:

"AND" or "OR" or "NOT" in str1


How does a human reader understand that as English?

if "AND" is in the string, or "OR" is in the string, or "NOT"
is in the string, then it is a boolean query.

But sadly, that is not how Python sees it. Python sees it as:

if "AND" is a true value, or if "OR" is a true value,
or if "NOT" is in the string, then it is a boolean query.

"AND" is always a true value. All strings except for the empty string "" are
true values, so expressions like:

if X or Y or Z in some_string

will always be true, if X or Y are true values.

You need to re-write your test to be one of these:

# version 1
if "AND" in str1 or "OR" in str1 or "NOT" in str1:
print "It is a Boolean Query"
else:
print "It is not a Boolean Query"


# version 2
if any(word in str1 for word in ("AND", "OR", "NOT")):
print "It is a Boolean Query"
else:
print "It is not a Boolean Query"




--
Steven

subhabrat...@gmail.com

unread,
Apr 24, 2015, 12:40:27 PM4/24/15
to
On Friday, April 24, 2015 at 8:45:04 PM UTC+5:30, Steven D'Aprano wrote:
Thanks Steven for taking so much of your valuable time to make me understand and nice guidance. Ian your suggestion worked well. Thank you. Regards, Subhabrata Banerjee.

Tim Chase

unread,
Apr 24, 2015, 1:36:19 PM4/24/15
to Ian Kelly, Python
On 2015-04-24 09:00, Ian Kelly wrote:
> It is not equivalent to:
>
> if ("AND" in str1) or ("OR" in str1) or ("NOT" in str1):

Which python allows you to write nicely as

if any(term in str1 for term in ["AND", "OR", "NOT"]):

The use of any()/all() has certainly improved the readability of my
code since they were added (in 2.5?).

-tkc



Terry Reedy

unread,
Apr 24, 2015, 8:51:56 PM4/24/15
to pytho...@python.org
On 4/24/2015 10:50 AM, subhabrat...@gmail.com wrote:
> Dear Group,
>
> I am trying to understand the use of Boolean operator in Python. I am trying to
> write small piece of script, as follows,
>
> def input_test():
> str1=raw_input("PRINT QUERY:")
> if "AND" or "OR" or "NOT" in str1:
> print "It is a Boolean Query"
> elif "AND" or "OR" or "NOT" not in str1:
> print "It is not a Boolean Query"
> else:
> print "None"

> I am trying to achieve if the words "AND","OR","NOT" ... in string I
want to recognize it as,
> Boolean Query, if they are not there the string is not Boolean query.
> As I am trying to test it I am getting following output,
>
>>>> input_test()
> PRINT QUERY:obama AND clinton
> It is a Boolean Query

>>>> input_test()
> PRINT QUERY:obama clinton
> It is a Boolean Query
---

Functions that do real calculation should be written as functions that
take an input object (or objects) and produce an output object. You can
then write automated tests can be run repeatedly.

Retyping the function name (multiple times) and test strings every time
you modify and retest the function is tedious and *subject to error*.
It is easy to write simple test functions. Example implementing both ideas:


def is_bool_text(text):
if "AND" or "OR" or "NOT" in text:
return True
elif "AND" or "OR" or "NOT" not in text:
return False

def test_is_bool_text():
for inp, expect in (('obama AND clinton', True),
('obama clinton', False),
):
print(inp, 'PASS' if is_bool_text(inp) == expect else 'FAIL')

test_is_bool_text()

Currently, the second example is FAIL. You can now modify the function
and re-run the test until both examples PASS. Then add more test cases.

Based on my experience reading newbie posts on python list and
Stackoverflow, learning to write real functions, without input and
print, and repeatable tests, is the most important thing many people are
not learning from programming books and classes.

--
Terry Jan Reedy

Rustom Mody

unread,
Apr 24, 2015, 9:15:00 PM4/24/15
to
On Saturday, April 25, 2015 at 6:21:56 AM UTC+5:30, Terry Reedy wrote:
> Functions that do real calculation should be written as functions that
> take an input object (or objects) and produce an output object.

<example snipped>

> Based on my experience reading newbie posts on python list and
> Stackoverflow, learning to write real functions, without input and
> print, and repeatable tests, is the most important thing many people are
> not learning from programming books and classes.

Hear Hear! Hail Terry!!

I believe what newbies dont get is the separation of 'real calculation'
and other clerical/logistical stuff

Mark Lawrence

unread,
Apr 24, 2015, 9:22:53 PM4/24/15
to pytho...@python.org
On 25/04/2015 01:51, Terry Reedy wrote:
>
> Based on my experience reading newbie posts on python list and
> Stackoverflow, learning to write real functions, without input and
> print, and repeatable tests, is the most important thing many people are
> not learning from programming books and classes.
>

Got to start them off somewhere so http://nedbatchelder.com/text/test0.html

--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

Rustom Mody

unread,
Apr 25, 2015, 1:04:31 AM4/25/15
to
On Saturday, April 25, 2015 at 6:52:53 AM UTC+5:30, Mark Lawrence wrote:
> On 25/04/2015 01:51, Terry Reedy wrote:
> >
> > Based on my experience reading newbie posts on python list and
> > Stackoverflow, learning to write real functions, without input and
> > print, and repeatable tests, is the most important thing many people are
> > not learning from programming books and classes.
> >
>
> Got to start them off somewhere so http://nedbatchelder.com/text/test0.html

Nice! Thanks for that!
Found some other gems from Ned which Ive stuffed into my blog -- will put it in
the relevant thread

However I dont believe that Ned and Terry are talking the same thing -- though
closely related.
Ned is talking of how to do testing
Terry is talking of how to write clean (old-fashioned word 'structured') code
and using testing as an allurement.
They are not the same because if (say!) you were a genius who wrote
bug-free code on first attempt you dont need to test; you still need to write
clean code for others to read

Albert van der Horst

unread,
May 8, 2015, 5:15:11 AM5/8/15
to
In article <553a5ded$0$12978$c3e8da3$5496...@news.astraweb.com>,
Steven D'Aprano <steve+comp....@pearwood.info> wrote:
>On Sat, 25 Apr 2015 12:50 am, subhabrat...@gmail.com wrote:
>
>> Dear Group,
>>
>> I am trying to understand the use of Boolean operator in Python. I am
>> trying to write small piece of script, as follows,
>>
>> def input_test():
>> str1=raw_input("PRINT QUERY:")
>> if "AND" or "OR" or "NOT" in str1:
>> print "It is a Boolean Query"
>> elif "AND" or "OR" or "NOT" not in str1:
>> print "It is not a Boolean Query"
>> else:
>> print "None"
>
>First problem: why do you sometimes return "None"? You have two possible
>answers: either something is a boolean query, or it is not. There is no
>third choice. ("It's a boolean query, but only on Wednesdays.")

In the context of testcode where the OP is not knowing what is going on
it is absolutely useful, and legit. His problem is that it is Wednesday and
weird things happen.


>Steven
>
--
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- being exponential -- ultimately falters.
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

0 new messages