How do I use a Boolean in Python?(如何在 Python 中使用布尔值?)
问题描述
Python 是否真的包含布尔值?我知道你可以这样做:
Does Python actually contain a Boolean value? I know that you can do:
checker = 1
if checker:
#dostuff
但我很迂腐,喜欢在 Java 中看到布尔值.例如:
But I'm quite pedantic and enjoy seeing booleans in Java. For instance:
Boolean checker;
if (someDecision)
{
checker = true;
}
if(checker)
{
//some stuff
}
Python 中是否有布尔值之类的东西?我似乎在文档中找不到类似的东西.
Is there such a thing as a Boolean in Python? I can't seem to find anything like it in the documentation.
推荐答案
checker = None
if some_decision:
checker = True
if checker:
# some stuff
更多信息:http://docs.python.org/library/functions.html#bool
您的代码也可以工作,因为必要时 1 会转换为 True.实际上 Python 很久没有布尔类型了(就像在旧的 C 中一样),一些程序员仍然使用整数而不是布尔值.
Your code works too, since 1 is converted to True when necessary.
Actually Python didn't have a boolean type for a long time (as in old C), and some programmers still use integers instead of booleans.
这篇关于如何在 Python 中使用布尔值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 Python 中使用布尔值?
基础教程推荐
- 求两个直方图的卷积 2022-01-01
- 包装空间模型 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
