关于真实价值的问题 [英] question about True values
问题描述
但它在第二部分呢?第一个陈述是不同的吗?
>> s =''hello''
s == True
False
>> if s:
print''hi ''
hi
>>>
谢谢。
John Salerno< ; jo ****** @ NOSPAMgmail.com写:
我有点困惑。为什么在第一个
部分中没有评价为True,但是它在第二部分呢?第一个声明是什么
不同?
编号True和False是布尔值,其中布尔值是来自字符串的不同的数据类型,就像字符串与整数不同。
>> if s:
print''hi''
在评估期间将s转换为布尔值。也就是说,它与
相同如果bool(s):print''hi''
bool(s)是一个将s转换为bool的函数。如果s是一个字符串,
bool(s)如果s是非空的则为true,否则为false。
与整数相似的东西:假设
x = 3.1
然后x == 3是假的,但是int(x)== 3是的。
John我有点困惑。为什么在第一个
Johnpart中没有评价为True,但它在第二个时间是否存在?第一个陈述是什么
Johndifferent?
>> ; s =''你好''
s == True
False
>> if s:
...打印''hi''
hi
s不等于布尔对象True,但它也不是评估为
字符串类''nil"值。每个内置类型都有这样一个空白的bb空。或nil或nil值:
string"
list []
元组()
dict {}
int 0
float 0.0
complex 0j
set set()
除上述之外的任何其他值将比较为非假。
跳过
John Salerno写道:
我有点困惑。为什么在第一部分没有评价为True,
但它在第二部分呢?第一个陈述是不同的吗?
>> s =''hello''
>> s == True
False
>> if s:
print''hi''
hi
>>>
谢谢。
优秀的问题!这应该有所帮助:
>> s =" Hello"
s
''Hello''
>> bool(s)
True
< blockquote class =post_quotes>
>> s == True
False
s的值不等于True的值。但是,s的* boolean *
值为True,因为它不是0或空字符串。 python
''if''语句计算条件表达式的布尔值。
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.
John Salerno <jo******@NOSPAMgmail.comwrites: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.
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
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.
这篇关于关于真实价值的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!