python - 'number % 2:' 和 'number % 2 == 0' 之间的区别?

标签 python python-3.x

我正在学习 Python 的 bool 逻辑以及如何简化逻辑。标题中的两个表达是等价的吗?如果不是,它们之间有什么区别?

最佳答案

number % 2 如果 number 是偶数则为 0(所以为 False)

number % 2 == 0 是 True 是偶数

第一个返回 int,第二个返回 bool。 Python 的 truthiness不过,您可以用同样的方式处理它们。

关于python - 'number % 2:' 和 'number % 2 == 0' 之间的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31837170/

相关文章:

java - JayDeBeApi 连接崩溃

python - 将整个 Python 应用程序打包为 PEX 文件

python - 使用生成器分块读取 CSV 文件

python - 如何使用 SQLAlchemy 创建带有 JSON 列的 SQLite 表?

python - 如何将类添加到 Django 国家/地区小部件

python - 从 Pandas 中的 iterrows() 获取行位置而不是行索引

python - social-auth-app-django:刷新 access_token

python - 这些集合操作是什么,为什么它们会给出不同的结果?

python - 用于 isinstance() 检查的 dict_keys 的显式 python3 类型是什么?

python - 当不是每个人都有中间名时,将名字解析为名字、中间名、姓氏