473,695 Members | 2,603 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

question about True values

I'm a little confused. Why doesn't s evaluate to True in the first part,
but it does in the second? Is the first statement something different?
>>s = 'hello'
s == True
False
>>if s:
print 'hi'
hi
>>>
Thanks.
Oct 25 '06 #1
90 3420
John Salerno <jo******@NOSPA Mgmail.comwrite s:
I'm a little confused. Why doesn't s evaluate to True in the first
part, but it does in the second? Is the first statement something
different?
No. True and False are boolean values, where booleans are a different
data type from strings, just like strings are different from integers.
>>if s:
print 'hi'

converts s to a boolean during evaluation. That is, it's the same as

if bool(s): print 'hi'

bool(s) is a function that converts s to a bool. If s is a string,
bool(s) is true if s is nonempty, false otherwise.

A comparable thing with integers: suppose

x = 3.1

then "x == 3" is false, but "int(x) == 3" is true.
Oct 25 '06 #2

JohnI'm a little confused. Why doesn't s evaluate to True in the first
Johnpart, but it does in the second? Is the first statement something
Johndifferent?
>>s = 'hello'
s == True
False
>>if s:
... print 'hi'
hi

s is not equal to the boolean object True, but it also doesn't evaluate to
the string class's "nil" value. Each of the builtin types has such an
"empty" or "nil" value:

string ""
list []
tuple ()
dict {}
int 0
float 0.0
complex 0j
set set()

Any other value besides the above will compare as "not false".

Skip
Oct 25 '06 #3

John Salerno wrote:
I'm a little confused. Why doesn't s evaluate to True in the first part,
but it does in the second? Is the first statement something different?
>>s = 'hello'
>>s == True
False
>>if s:
print 'hi'
hi
>>>

Thanks.
Excellent question! This should help:
>>s = "Hello"
s
'Hello'
>>bool(s)
True
>>s == True
False

The value of s is not equal to the value of True. But, the *boolean*
value of s is True, since it is not 0 or an empty string. The python
'if' statement evaluates the boolean value of the condition expression.

Oct 25 '06 #4

Paul Rubin wrote:
John Salerno <jo******@NOSPA Mgmail.comwrite s:
I'm a little confused. Why doesn't s evaluate to True in the first
part, but it does in the second? Is the first statement something
different?

No. True and False are boolean values, where booleans are a different
data type from strings, just like strings are different from integers.
>>if s:
print 'hi'

converts s to a boolean during evaluation. That is, it's the same as

if bool(s): print 'hi'

bool(s) is a function that converts s to a bool. If s is a string,
bool(s) is true if s is nonempty, false otherwise.

A comparable thing with integers: suppose

x = 3.1

then "x == 3" is false, but "int(x) == 3" is true.
But then why is 3.0 == 3 true? They are different types.

Oct 25 '06 #5
Paul Rubin wrote:
No. True and False are boolean values, where booleans are a different
data type from strings, just like strings are different from integers.
>>if s:
print 'hi'

converts s to a boolean during evaluation.

Oh!!! I get it now! I was thinking that

if s

was the same as

if s == True

because I know sometimes you can write if statements this way (though
it's wordy). But what I didn't realize was that in the cases I was
thinking of, 's' was an expression that evaluated to a boolean value,
not an actual value of some other type!

So I suppose

if (10 5)

would be the same as

if (10 5) == True

because (10 5) does evaluate to "True".
Oct 25 '06 #6
"John Coleman" <jc******@franc iscan.eduwrites :
then "x == 3" is false, but "int(x) == 3" is true.
But then why is 3.0 == 3 true? They are different types.
The 3 gets converted to float, like when you say

x = 3.1 + 3

the result is 6.1.
Oct 25 '06 #7
John Salerno <jo******@NOSPA Mgmail.comwrite s:
if (10 5)
would be the same as
if (10 5) == True
Yes.
Oct 25 '06 #8
John Salerno wrote:
I'm a little confused. Why doesn't s evaluate to True in the first part,
but it does in the second? Is the first statement something different?
>>s = 'hello'
>>s == True
False
>>if s:
print 'hi'
hi
>>>
They are, indeed, quite different things. Finding the truth value of an object
is not the same thing as testing if the object is equal to the object True. When
people use the phrase "evaluate to True" in the context of an if: statement,
they mean that the truth value of the object (found using the special method
..__nonzero__() ) is True, not that it is equal to True.

It's a bit of an abuse on the English language, but what isn't in software?

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Oct 25 '06 #9
Robert Kern wrote:
They are, indeed, quite different things. Finding the truth value of an
object is not the same thing as testing if the object is equal to the
object True.
Yeah, I had this in the back of my mind, but I was thinking that this
test would be written as

if s is True

And I know that one is very different from the others.
Oct 25 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
6016
by: hedylogus | last post by:
I've just begun learning C++ and I'm trying to write a program to shuffle a deck of cards. I've succeeded....for the most part....but every now and then rand() produces duplicate random numbers causing me to "lose" a card. How do I avoid this??? I've attached my code below for reference. Thanks in advance. ------------------------ #include <iostream> #include <cstdlib> #include <ctime>
38
5071
by: Shaun McKinnon | last post by:
HI...Here's my problem...I have a popup window that loads when i want it to, but it's not sized properly. I've set the size, but it doesn't seem to work. I've been on 8 different websites to find out what i'm doing wrong, and so far it seems i'm doing it the right way. Here's my code...any suggestions would be appreciated. <script language="javascript"> <!-- window.open("256fx/index.htm", "", "height=400, width=600"); //-->
5
2683
by: Sue | last post by:
After finishing up my first quarter JavaScript on 12/12/03, I decided to improve character checking on my project. In my project I only had to do very basic validation. Therefore, I only had one function to verify the name fields, age, email and gender. My question is: if I create a function for each field like the code below, what would be the best way to organize the functions and call them? Would I need one main function and place...
22
1946
by: James H. | last post by:
Greetings! I'm new to Python and am struggling a little with "and" and "or" logic in Python. Since Python always ends up returning a value and this is a little different from C, the language I understand best (i.e. C returns non-zero as true, and zero as false), is there anything I should be aware of given Python's different approach? Namely any pitfalls or neat tricks that make the Python approach cool or save my butt. Thank you!
14
1835
by: Mr Newbie | last post by:
I am often in the situation where I want to act on the result of a function, but a simple boolean is not enough. For example, I may have a function called isAuthorised ( User, Action ) as ????? OK, this function may return a boolean, and if this is true, then no message back is really required, but if it fails then some supporting message needs to be returned to the calling code. As I see it there are a few options.
18
2860
by: Nobody | last post by:
I've been looking for a job for a while now, and have run into this interview question twice now... and have stupidly kind of blown it twice... (although I've gotten better)... time to finally figure this thing out... basically the interview question is: given an unsorted listed such as: 3,1,3,7,1,2,4,4,3 find the FIRST UNIQUE number, in this case 7... and of course the list can be millions long...
4
2878
by: RobG | last post by:
I have always accessed attributes such as disabled using the DOM element property, however I was wondering about implementing a more generic function to get the values of attributes - which of course leads to the DOM Element Interface's getAttribute method: <URL: http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-666EE0F9 > The DOM 2 Core specification says that getAttribute should return the value of an attribute as a string, however...
2
1287
by: cbmeeks | last post by:
Man, I realize I've been asking a lot of questions lately. I appreciate you guys helping me in my thought process. This time, I have a design question I want opinions on. I'm building a business framework for tracking trending data. I have a class like: class Metric
1
1862
by: deanchhsw | last post by:
Part A (http://bytes.com/topic/java/insights/645821-sudoku) B (http://bytes.com/topic/java/insights/739704-sudoku-b) C (http://bytes.com/topic/java/insights/739703-sudoku-c) this question refers to the Sudoku howto posted on this website. The links are shown above. My question centers around the boolean variables declared in Part A, such as the following. boolean rows= new boolean; ...
0
8647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9004
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8838
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6506
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5842
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4351
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4592
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3024
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2288
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.