python - 如何正确使用括号?
问题描述
为什么它的工作方式不同?
p='/content/Images_of_Waste/img/PET968.txt'
p[-3:]!='txt' and p[-3:]!='jpg'
False
p[-3:]!=('txt' and 'jpg')
True
如何正确使用括号?
解决方案
在 Python 中,非空字符串实际上是True
.
也就是说,
if 'txt':
# this code will execute
然而,正如@gimix 下面提到的那样,
if 'txt' == True:
# this code will not execute
就('txt' and 'jpg')
, 'txt'
is notFalse
和not 而言'jpg'
,因此,根据@Manish 的评论('txt' and 'jpg')
进行评估。'jpg'
推荐阅读
- react-native - 当 Modal 可见时,有没有办法与背景组件进行交互?
- html - 如何在离子3中的离子项目中添加彩色左上角弯曲三角形
- mysql - MySQL - 如何在游标的where子句中使用变量
- azure - 如何在 Azure 门户中自行离开目录?
- common-table-expression - Snowflake 中的递归 CTE 循环
- c# - .net 中的 swashbuckle cli 和 nswag 中的可空引用类型
- android - 我无法在 android studio 2.1.1 中构建我创建的应用程序
- c - 将可变参数 arg 列表 (...) 传递给另一个函数时出现问题
- python - 我如何知道函数应该解包多少个值?
- ansible - 如何将列表中的 Ansible 注册变量存储到单独的变量中