数字和真值 [英] Numbers and truth values

查看:80
本文介绍了数字和真值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新手问题:


为什么1 ==真,2 = =真(即使1!= 2),

但是''x ''!= True(即使''x'':有效)?

解决方案

Szabolcs写道:


为什么1 ==真,2 ==真(即使1!= 2),



不是我得到的。


Python 2.5(r25:51908,2007年3月13日,08:13:14)

[GCC 3.4.4(cygming special,gdc 0.12,使用dmd) 0.125)] on cygwin

输入help,copyright,credit等等。或许可证或欲获得更多信息。


>> 2 == True



False

-

Michael Hoffman


Szabolcs写道:


新手问题:


为什么1 ==真,2 ==真(即使1! = 2),

但''x''!=真(即使''x'':有效)?



请在发布前检查:


[E:\Projects] python

在Firefox32上的Python 2.5.1(r251:54863,2007年4月18日,08:51:08)[MSC v.1310 32位(英特尔)]


>> 2 == True



False


>> ;>



无论如何,

有助于考虑价值域(在你的例子,数字和字符串中)

有一个单独的null值和非空值。

空值被视为False。

非空值被视为真。


数字的空值显然为0,对于字符串,它是''''
(空字符串)。

非零数字和非空数字字符串被认为是真字符串当在布尔表达式中使用

时。


--Irmen


4月28日星期六2007 14:33:23 +0200,Szabolcs写道:


新手问题:


为什么1 == True和2 == True(即使1!= 2),

但是''x''!= True(即使''x'':有效)?



Python中的所有东西都有真值。所以你总能这样做:


如果some_object:

print" if clause is true"

else:

print" else子句"


无论some_object是什么。


常量True和False是一对特殊类型bool的值。

bool类型实际上是int的子类:

< blockquote class =post_quotes>
>> issubclass(bool,int)



True


>> 7 + False



7


>> 7 + True



8


你能猜出什么值真与假是引人注目的?


>> 1 == True



True


>> 0 == False



True


>> 2 == True



False


>> if 2:



.... print" 2被认为是真的"

....

2被认为是真的

如果你得到不同的结果,你可能有
意外重新分配名称判断对错:


>> ;真= 2#不要这样做!!!
2 ==真





-

史蒂文。


Newbie question:

Why is 1 == True and 2 == True (even though 1 != 2),
but ''x'' != True (even though if ''x'': works)?

解决方案

Szabolcs wrote:

Why is 1 == True and 2 == True (even though 1 != 2),

Not what I get.

Python 2.5 (r25:51908, Mar 13 2007, 08:13:14)
[GCC 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)] on cygwin
Type "help", "copyright", "credits" or "license" for more information.

>>2 == True

False
--
Michael Hoffman


Szabolcs wrote:

Newbie question:

Why is 1 == True and 2 == True (even though 1 != 2),
but ''x'' != True (even though if ''x'': works)?

Please check before you post:

[E:\Projects]python
Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.

>>2==True

False

>>>

Anyway,
it helps to think about value domains (in your examples, numbers and strings)
as having a single "null" value, and non-null values.
The null value is considered False.
The non-null values are considered True.

The null value for numbers is 0 obviously, and for strings it is ''''
(the empty string).
Non-zero numbers and non-empty strings are considered "True" when used
in a boolean expression.

--Irmen


On Sat, 28 Apr 2007 14:33:23 +0200, Szabolcs wrote:

Newbie question:

Why is 1 == True and 2 == True (even though 1 != 2),
but ''x'' != True (even though if ''x'': works)?

Everything in Python has a truth-value. So you can always do this:

if some_object:
print "if clause is true"
else:
print "else clause"

no matter what some_object is.

The constants True and False are a pair of values of a special type bool.
The bool type is in fact a sub-class of int:

>>issubclass(bool, int)

True

>>7 + False

7

>>7 + True

8

Can you guess what values True and False are "under the hood"?

>>1 == True

True

>>0 == False

True

>>2 == True

False

>>if 2:

.... print "2 is considered true"
....
2 is considered true
If you are getting different results, the chances are that you have
accidentally reassigned that names True or False:

>>True = 2 # DON''T DO THIS!!!
2 == True

True

--
Steven.


这篇关于数字和真值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆