> For the complete documentation index, see [llms.txt](https://hezhiqiang8909.gitbook.io/python-ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hezhiqiang8909.gitbook.io/python-ai/04_operator.md).

# 运算符

## 算数运算符

* 加：`+`
* 减：`-`
* 乘：`*`
* 除：`/`
* 整除, 地板除：`//`
* 取模：`%`
* 指数：`**`
* 方向：从右到左
* 优先级从小到大：`+,-,*,/,//,%,**`

## 赋值运算符

* 方向：从左到右
* 优先级从小到大：`=, +=, -=, *=, /=, //=, %=, **=`

## 比较运算符

* `a < b`
* `a > b`
* `a <= b`
* `a >= b`
* `a == b`
* `a != b`
* `a < b < c`
* 优先级从小到大：

## 逻辑运算符

* `and`
* `or`
* `not`

## 位运算符

* `&`
* `|`
* `^`
* `~`

## 成员运算符

* `in`
* `not in`

## 身份运算符

> 比较两个对象存储单元，即是不是引用同一个对象

* `is`
* `is not`

## 运算符优先级

* 从最高到最低优先级的所有运算符

```python
运算符           描述
**                指数 (最高优先级)
~ + -             按位翻转, 一元加号和减号 (最后两个的方法名为 +@ 和 -@)
* / % //          乘，除，取模和取整除
+ -               加法减法
>> <<             右移，左移运算符
&                 位 'AND'
^ |               位运算符
<= < > >=         比较运算符
<> == !=          等于运算符
= %= /= //= -= += *= **=   赋值运算符
is is not         身份运算符
in not in         成员运算符
not or and        逻辑运算符
```

```python
'''  x or y x Ture, return x '''
print(1 or 2) # 1
print(3 or 2) # 3
print(0 or 2) # 2
print(0 or 100) # 100

''' x and y x true,return y '''
print(1 and 2) # 2
print(0 and 2) # 0

print(0 or 4 and 3 or 2) # 优先级 4 and 3, 0 or 3 or 2 => 2

print(1 > 2 and 3 or 4 and 3 < 2) # False
print(2 or 1 < 3 and 2) # 2
```
