新Sir

导航

 

在Python中,"or"是一个逻辑运算符,用于连接两个布尔表达式,并返回第一个为True的表达式。以下是"or"运算符的用法:

1. 判断两个布尔表达式是否都为True,如果都为True,则返回True,否则返回False。


```python
a = True
b = False
if a or b:
print("At least one of a or b is True.")
```
输出:At least one of a or b is True.
2. 判断第一个表达式是否为True,如果是,则返回该表达式,否则判断第二个表达式是否为True。


```python
a = True
b = False
x = 1 if a or b else 0
print(x)
```
输出:1
3. 可以将多个"or"运算符连接在一起,用于判断多个条件的组合。


```python
a = True
b = False
c = True
if a or b or c:
print("At least one of a, b or c is True.")
```
输出:At least one of a, b or c is True.
4. 可以与其他逻辑运算符一起使用。


```python
a = True
b = False
if a or (b and c):
print("At least one of a or (b and c) is True.")
```
输出:At least one of a or (b and c) is True.

posted on 2023-11-02 14:05  新Sir  阅读(210)  评论(0编辑  收藏  举报