473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 1121
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

99
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less...
9
by: cjl | last post by:
Hey all: I am working on a little script that needs to pull the strings out of a binary file, and then manipulate them with python. The command line utility "strings" (part of binutils) has...
5
by: kurt sune | last post by:
The code: Dim aLine As String = "cat" & vbNewLine & "dog" & vbNewLine & "fox" & vbNewLine Dim csvColumns1 As String() = aLine.Split(vbNewLine, vbCr, vbLf) Dim csvColumns2 As String() =...
135
by: Xah Lee | last post by:
Tabs versus Spaces in Source Code Xah Lee, 2006-05-13 In coding a computer program, there's often the choices of tabs or spaces for code indentation. There is a large amount of confusion about...
6
subashini Thiyagarajan
by: subashini Thiyagarajan | last post by:
Hi, Can any one have strong idea on the distance supported by CAT 5 and CAT 6. one pc and switch
1
by: bdub7989 | last post by:
Hi, I am about as green as they come in using VBA and have encountered an area that leaves me scratching my head. Below is my situation and what I would like to do; I just don't really know how to...
3
by: gizz | last post by:
Hi, Is there a difference between : var myStr = "This is some text" ; and, var myStr = 'This is some text' ;
24
by: Donn Ingle | last post by:
Hello, I hope someone can illuminate this situation for me. Here's the nutshell: 1. On start I call locale.setlocale(locale.LC_ALL,''), the getlocale. 2. If this returns "C" or anything...
22
cat
by: Jag | last post by:
I've read parts of K&R's ANSI C v2 and this is what their cat looked like but when I compared the speed of this code to gnu cat, it seems very slow. How do I optimize this for greater speeds? is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.