简体   繁体   English

在Python中将元组与字符串进行比较

[英]Compaing tuple to string in python

I have spent couple hours trying to figure out how tuples and strings are compared in Python. 我花了几个小时试图弄清楚在Python中如何比较元组和字符串。 I have a theory, but just wanted to check if I was correct. 我有一个理论,但只想检查我是否正确。

Given the code: 给定代码:

 if('michael' is 'hello',):
     print "true"

why does this always return true? 为什么总是返回true?

'michael' is 'hello' is obviously False, so the result is if (False,): . 'michael' is 'hello'显然是False,所以结果是if (False,): A tuple doesn't care what's inside; 元组不在乎里面是什么; it has a boolean value of True if there is just at least one something in there, and False if not. 如果其中至少有一个东西,则其布尔值为True,否则为False。 Even though the only item there is False, it returns True because at least there's something. 即使唯一的项目为False,它也会返回True,因为至少有一些东西。 If you want to see if every item is True, use if all(('michael' is 'hello',)): 如果要查看每个项目是否为True,请使用if all(('michael' is 'hello',)):

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM