python - 在单个语句中在 python 中多次测试单个变量

标签 python python-2.7 syntax conditional-statements logical-operators

我在大学学习 Python,并了解了逻辑运算符和条件语句的工作原理。我的问题是:有没有什么方法可以压缩这样的代码?

if (day != "Sunday" and day != "Saturday" and day != "Friday" and day != "Thursday" and day != "Wednesday" and day != "Tuesday" and day != "Monday"):
    print "That is not a day"
    return 0

欢迎任何建议,我知道上面的例子很简单。

最佳答案

您可以使用 in 关键字。

if day not in ("Sunday", "Saturday", "Friday", "Thursday", "Wednesday", "Tuesday", "Monday"):

关于python - 在单个语句中在 python 中多次测试单个变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26342446/

相关文章:

python - 使用 python 从文本中获取个人和组织名称的方法

python - 比较运算符之间没有隐含关系

delphi - 为什么我不能对类使用运算符重载?

python - SqlAlchemy 和多处理

Python 输入 : Use a class variable's value as return type of a (mixin) method

python - 带有缺失/不完整标题或不规则列数的 read_csv

Python:BeautifulSoup 中的 find 在一台 Windows 计算机上工作正常,但在另一台计算机上工作不正确

php - 从图像对象输出图像 URL 作为背景图像

php - PHP 和 HTML 中引号的语法问题

python - 我想填写一个文本框,然后使用 python 单击提交按钮