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

S

Sumitava Mukherjee

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

I get this:
True

[This is ok because (any) string is True, so; (True and True) gives
True]


Devil

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


What is python doing when we type in ('God' and 'Devil') or key in (01
and 10) ?
 
B

Benjamin

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

I get this:

True

[This is ok because (any) string is True, so; (True and True) gives
True]

Devil

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

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/expressions.html#boolean-operations)
 
Z

zaarg

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

I get this:

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]

Devil

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

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/expressions.html#binary-bitwise-operations
e.g.
 
S

Steven D'Aprano

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)
 
T

Tim Roberts

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

I get this:
True

[This is ok because (any) string is True, so; (True and True) gives
True]

Your statement is accurate, but that's not why it returns true.

Right. And bool('Devil') is True, because a non-empty string is a true
value.
[This is what I don't get ]
and for that matter,I also checked out this:
10

What is python doing when we type in ('God' and 'Devil') or key in (01
and 10) ?

"and" and "or" are a bit more than logical operators. The exact definition
of "x and y" is "if x has a false value, return x, otherwise return y". If
both sides are booleans, this does exactly what you expect.

Similarly, "x or y" is actually done as "if x has a true value, return x,
otherwise return y".

This allows for one of the cuter Python hacks:
xxx = x and y or z
which is essentially the same as the C ternary operator:
xxx = x ? y : z;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,835
Latest member
KetoRushACVBuy

Latest Threads

Top