180 lines
5.9 KiB
Markdown
180 lines
5.9 KiB
Markdown
# Python 布尔和条件编程:如果..其他
|
||
|
||
> 原文:[https://python . land/introduction-to-python/python-boolean-and-operators](https://python.land/introduction-to-python/python-boolean-and-operators)
|
||
|
||
除了[数字](https://python.land/python-data-types/python-integer)和[字符串](https://python.land/introduction-to-python/strings),Python 还有其他几种类型的数据。其中之一是布尔数据类型。布尔值非常简单:它们要么为真,要么为假。布尔与布尔运算符相结合,使得创建条件程序成为可能:基于某些条件决定做不同事情的程序。
|
||
|
||
布尔数据类型是以乔治·布尔的名字命名的,他在 19 世纪中期定义了一个逻辑代数系统。
|
||
|
||
目录
|
||
|
||
|
||
|
||
* [什么是布尔?](#What_is_a_Boolean "What is a Boolean?")
|
||
* [Python 运算符](#Python_operators "Python operators")
|
||
* [比较 Python 中的不同类型](#Comparing_different_types_in_Python "Comparing different types in Python")
|
||
|
||
|
||
|
||
## 什么是布尔?
|
||
|
||
让我们从一个定义开始:
|
||
|
||
**Boolean**
|
||
|
||
A boolean is the simplest data type; it’s either `True` or `False`.
|
||
|
||
在计算机科学中,布尔被大量使用。这与计算机内部的工作方式有关。计算机内部的许多操作可以归结为一个简单的“对或错”值得注意的是,在 Python 中,布尔值以大写字母开始:`True`或`False`。这与大多数其他编程语言形成对比,在其他编程语言中,小写是标准。
|
||
|
||
在 Python 中,我们结合使用布尔和条件语句来控制程序的流程:
|
||
|
||
```py
|
||
>>> door_is_locked = True
|
||
>>> if door_is_locked:
|
||
... print("Mum, open the door!")
|
||
...
|
||
Mum, open the door!
|
||
>>>_
|
||
```
|
||
|
||
下面是相同代码的交互式版本,您可以进行试验:
|
||
|
||
[https://crumb . sh/embed/tfax 2y xobtk](https://crumb.sh/embed/TfAX2YXobTK)
|
||
|
||
Python if 语句
|
||
|
||
首先,我们定义一个名为`door_is_locked`的变量,并将其设置为`True`。接下来,您将找到一个 if 语句。这就是所谓的条件语句。它后面是一个表达式,可以计算为`True`或`False`。如果表达式的计算结果为`True`,则执行后面的代码块。如果评估为`False`,则跳过。继续将`door_is_locked`改为`False`,看看会发生什么。
|
||
|
||
if 后面可以跟一个可选的 else 块。仅当表达式计算结果为`False`时,才执行该块。这样,您可以为这两个选项运行代码。让我们试试这个:
|
||
|
||
```py
|
||
>>> door_is_locked = False
|
||
>>> if door_is_locked:
|
||
... print("Mum, open the door!")
|
||
... else:
|
||
... print("Let's go inside")
|
||
...
|
||
Let's go inside
|
||
>>>_
|
||
```
|
||
|
||
感谢我们的 else 块,如果`door_is_locked`是`False`,我们现在可以打印一个替代文本。作为练习,尝试修改上面的交互式代码示例以获得相同的结果。
|
||
|
||
## Python 运算符
|
||
|
||
使用条件的能力是计算机运行的动力;它们使你的软件变得智能,并允许它根据外部输入改变自己的行为。到目前为止,我们已经直接使用了`True`,但是更多的表达式评估为`True`或`False`。这些表达式通常包含一个所谓的运算符。
|
||
|
||
Thank you for reading my tutorials. I write these in my free time, and it requires a lot of time and effort. I use ads to keep writing these *free* articles, I hope you understand! **Support me by disabling your adblocker on my website** or, alternatively, **[buy me some coffee](https://www.buymeacoffee.com/pythonland)**. It's much appreciated and allows me to keep working on this site!
|
||
|
||
有多种类型的操作符,现在,我们只看这些:
|
||
|
||
1. 比较运算符:它们比较两个值
|
||
2. 逻辑运算符
|
||
|
||
### 比较运算符
|
||
|
||
我们先来看看比较运算符。你可以在 REPL 和它们玩耍:
|
||
|
||
```py
|
||
>>> 2 > 1
|
||
True
|
||
>>> 2 < 1
|
||
False
|
||
>>> 2 < 3 < 4 < 5 < 6
|
||
True
|
||
>>> 2 < 3 > 2
|
||
True
|
||
>>> 3 <= 3
|
||
True
|
||
>>> 3 >= 2
|
||
True
|
||
>>> 2 == 2
|
||
True
|
||
>>> 4 != 5
|
||
True
|
||
>>> 'a' == 'a'
|
||
True
|
||
>>> 'a' > 'b'
|
||
False
|
||
```
|
||
|
||
这就是所有比较运算符的名称:
|
||
|
||
| 操作员 | 意义 |
|
||
| --- | --- |
|
||
| > | 大于 |
|
||
| < | 小于 |
|
||
| >= | 大于或等于 |
|
||
| <= | 小于或等于 |
|
||
| == | 是相等的 |
|
||
| != | 不相等 |
|
||
|
||
Python 的布尔运算符
|
||
|
||
从示例中可以看出,这些操作符也适用于字符串。字符串按照字母表的顺序进行比较,增加了以下规则:
|
||
|
||
* 大写字母比小写字母“小”,例如:“M”
|
||
* 数字小于字母:“1”
|
||
|
||
您可能想知道这些规则背后的逻辑是什么。在内部,每个字符在一个表格中都有一个数字。该表中的位置决定了顺序。就这么简单。如果你感兴趣的话,可以在维基百科上查看 Unicode。
|
||
|
||
### 逻辑运算符
|
||
|
||
接下来:逻辑运算符。这些操作符只对布尔值起作用,用于实现逻辑。下表列出并描述了它们:
|
||
|
||
| 操作员 | 什么是做 | 例子 |
|
||
| 和 | 如果两种说法都是真的 | 真与假==假
|
||
假与假==假
|
||
真与真==真 |
|
||
| 或者 | 如果其中一个陈述是正确的 | 真或假==真
|
||
真或真==真
|
||
假或假==假 |
|
||
| 不 | 否定下面的语句 | 不真==假
|
||
不假==真 |
|
||
|
||
Python 逻辑运算符
|
||
|
||
以下是 REPL 的一些例子,可以帮助你使用它们:
|
||
|
||
```py
|
||
>>> not True
|
||
False
|
||
>>> not False
|
||
True
|
||
>>> True and True
|
||
True
|
||
>>> True and False
|
||
False
|
||
```
|
||
|
||
## 比较 Python 中的不同类型
|
||
|
||
当你试图比较不同的类型时,你经常会得到一个错误。假设你想比较一个整数和一个字符串:
|
||
|
||
```py
|
||
>>> 1 < 'a'
|
||
Traceback (most recent call last):
|
||
File "<stdin>", line 1, in <module>
|
||
TypeError: '<' not supported between instances of 'int' and 'str'
|
||
>>>
|
||
```
|
||
|
||
这就是 Python 告诉你它不能比较整数和字符串的方式。但也有可以混搭的类型。我建议不要这样做,因为这会让你的代码难以理解,但是为了便于演示,让我们来比较一下 boolean 和 int:
|
||
|
||
```py
|
||
>>> True == 1
|
||
True
|
||
>>> False == 0
|
||
True
|
||
>>> True + True
|
||
2
|
||
>>> False + False
|
||
0
|
||
>>> False + True
|
||
1
|
||
>>> True + 3
|
||
4
|
||
>>>
|
||
```
|
||
|
||
可以看出,`True`的值为 1,`False`的值为 0。这与 Python 中布尔的内部表示有关:它们是 Python 中一种特殊的数字。 |