469,311 Members | 2,447 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,311 developers. It's quick & easy.

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 1053
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.

Similar topics

99 posts views Thread by David MacQuigg | last post: by
9 posts views Thread by cjl | last post: by
5 posts views Thread by kurt sune | last post: by
135 posts views Thread by Xah Lee | last post: by
3 posts views Thread by gizz | last post: by
cat
22 posts views Thread by Jag | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.