472,119 Members | 1,939 Online

# How is the logical processing being done for strings like 'Dog' and'Cat'

Hi all,
I am a novice programmer in Python.
Please could you explain me the results (regarding logical operators).

I get this:
>>print bool('God' and 'Devil')
True

[This is ok because (any) string is True, so; (True and True) gives
True]
>>print('God' and 'Devil')
Devil

[This is what I don't get ]
and for that matter,I also checked out this:
>>01 and 10
10
What is python doing when we type in ('God' and 'Devil') or key in (01
and 10) ?

Oct 21 '08 #1
3 1092
On Oct 20, 8:41*pm, Sumitava Mukherjee <sm...@cognobytes.comwrote:
Hi all,
I am a novice programmer in Python.
Please could you explain me the results (regarding logical operators).

I get this:
>print bool('God' and 'Devil')

True

[This is ok because (any) string is True, so; (True and True) gives
True]
>print('God' and 'Devil')

Devil

[This is what I don't get ]
and for that matter,I also checked out this:
>01 and 10

10

What is python doing when we type in ('God' and 'Devil') or key in (01
and 10) ?
This is an interesting property of python's logical operators. They
don't have to return a boolean object. (It's documented here:
http://docs.python.org/reference/exp...ean-operations)

Oct 21 '08 #2
On Oct 20, 9:41*pm, Sumitava Mukherjee <sm...@cognobytes.comwrote:
Hi all,
I am a novice programmer in Python.
Please could you explain me the results (regarding logical operators).

I get this:
>print bool('God' and 'Devil')

True

[This is ok because (any) string is True,
Not quite so. Be careful with this. The empty string gets False:
print bool("") --False
Any *non-empty* string gets True.
so; (True and True) gives
True]
>print('God' and 'Devil')

Devil

[This is what I don't get ]
and for that matter,I also checked out this:
>01 and 10

10
Note that AND is a boolean operator, not a bit operator. If you want
to hack bits of an integer, use the "binary bitwise operators"
http://docs.python.org/reference/exp...ise-operations
e.g.
>>01 & 10
0
>>01 | 10
11
>
What is python doing when we type in ('God' and 'Devil') or key in (01
and 10) ?
Oct 21 '08 #3
On Mon, 20 Oct 2008 18:41:23 -0700, Sumitava Mukherjee wrote:
What is python doing when we type in ('God' and 'Devil') or key in (01
and 10) ?

There are two important things you need to know:

(1) All Python objects have a boolean context.

(2) Python's boolean operators are short-circuit operators.
Point (1) means that you can say this:
if x:
print 'x has a true value'
else:
print 'x has a false value'

and it will work for any x, not just True and False.

As a general rule, false values are empty:

- the empty string is false: ''
- empty lists are false: []
- empty tuples are false: ()
- zeroes are false: 0, 0.0
- None is false
- etc.

and everything else is true.

So you can do this:

if alist:
# alist is not empty
x = alist[0] # so this is safe
else:
print 'alist is empty'
config = get_config_parser() # whatever that is...
if config:
do_stuff_with_config
else:
print "config is empty"

Point (2) means that Python will only evaluate the least number of
objects needed. So for example:

42 or 19

Since 42 is a true object, looking at the second item is pointless, and
Python short-circuits by returning 42.

0 or 19

Since 0 is a false object, Python must look at the second item. Since 19
is true, it returns 19.

And similarly for and.
This is especially useful with the or operator. Instead of this:

astring = something()
if len(astring) == 0:
astring = '<empty>'
do_something_with(astring)
you can do this:

astring = something() or '<empty>'
do_something_with(astring)

--
Steven
Oct 21 '08 #4

### This discussion thread is closed

Replies have been disabled for this discussion.