---
title: "python中的boolean"
author: "standalone"
date: "2009-09-28T09:55:34Z"
tags: []
slug: "python-boolean"
draft: false
lastmod: "2009-09-28T09:55:34Z"
id: 479054
url: "https://standalone.iteye.com/blog/479054"
---

在 2.2.1 版本之前，Python 没有单独的布尔数据类型。为了弥补这个缺陷，Python 在布尔环境 (如 `if` 语句) 中几乎接受所有东西，遵循下面的规则：

- `0` 为 false; 其它所有数值皆为 true。
- 空串 (`""`) 为 false; 其它所有字符串皆为 true。
- 空 list (`[]`) 为 false; 其它所有 list 皆为 true。
- 空 tuple (`()`) 为 false; 其它所有 tuple 皆为 true。
- 空 dictionary (`{}`) 为 false; 其它所有 dictionary 皆为 true。