版权所有,未经许可,禁止转载
章节
Python 介绍
Python 开发环境搭建
Python 语法
Python 变量
Python 数值类型
Python 类型转换
Python 字符串(String)
Python 运算符
Python 列表(list)
Python 元组(Tuple)
Python 集合(Set)
Python 字典(Dictionary)
Python If … Else
Python While 循环
Python For 循环
Python 函数
Python Lambda
Python 类与对象
Python 继承
Python 迭代器(Iterator)
Python 模块
Python 日期(Datetime)
Python JSON
Python 正则表达式(RegEx)
Python PIP包管理器
Python 异常处理(Try…Except)
Python 打开文件(File Open)
Python 读文件
Python 写文件
Python 删除文件与文件夹
Python 运算符
运算符用于对变量和值执行运算操作。
Python将运算符分为几类:
- 算术运算符
- 赋值运算符
- 比较运算符
- 逻辑运算符
- 身份运算符
- 成员运算符
- 位运算符
Python算术运算符
算术运算符与数值一起使用,执行常见的数学运算:
<table >
<tbody><tr>
<th style="width:25%">运算符</th>
<th style="width:35%">名称</th>
<th style="width:30%">示例</th>
</tr>
<tr>
<td>+</td>
<td>加</td>
<td>x + y</td>
</tr>
<tr>
<td>-</td>
<td>减</td>
<td>x - y</td>
</tr>
<tr>
<td>*</td>
<td>乘</td>
<td>x * y</td>
</tr>
<tr>
<td>/</td>
<td>除</td>
<td>x / y</td>
</tr>
<tr>
<td>%</td>
<td>取模</td>
<td>x % y</td>
</tr>
<tr>
<td>**</td>
<td>取幂</td>
<td>x ** y</td>
</tr>
<tr>
<td>//</td>
<td>向下取整除</td>
<td>x // y</td>
</tr>
</tbody></table>
Python赋值运算符
赋值运算符用于为变量赋值:
<table >
<tbody><tr>
<th style="width:25%">运算符</th>
<th style="width:20%">示例</th>
<th style="width:20%">等价于</th>
</tr>
<tr>
<td>=</td>
<td>x = 5</td>
<td>x = 5</td>
</tr>
<tr>
<td>+=</td>
<td>x += 3</td>
<td>x = x + 3</td>
</tr>
<tr>
<td>-=</td>
<td>x -= 3</td>
<td>x = x - 3</td>
</tr>
<tr>
<td>=</td>
<td>x = 3</td>
<td>x = x * 3</td>
</tr>
<tr>
<td>/=</td>
<td>x /= 3</td>
<td>x = x / 3</td>
</tr>
<tr>
<td>%=</td>
<td>x %= 3</td>
<td>x = x % 3</td>
</tr>
<tr>
<td>//=</td>
<td>x //= 3</td>
<td>x = x // 3</td>
</tr>
<tr>
<td>=</td>
<td>x **= 3</td>
<td>x = x ** 3</td>
</tr>
<tr>
<td>&=</td>
<td>x &= 3</td>
<td>x = x & 3</td>
</tr>
<tr>
<td>|=</td>
<td>x |= 3</td>
<td>x = x | 3</td>
</tr>
<tr>
<td>^=</td>
<td>x ^= 3</td>
<td>x = x ^ 3</td>
</tr>
<tr>
<td>>>=</td>
<td>x >>= 3</td>
<td>x = x >> 3</td>
</tr>
<tr>
<td><<=</td>
<td>x <<= 3</td>
<td>x = x << 3</td>
</tr>
</tbody></table>
Python比较运算符
比较运算符用于比较两个值:
<table >
<tbody><tr>
<th style="width:25%">运算符</th>
<th style="width:35%">名称</th>
<th style="width:30%">示例</th>
</tr>
<tr>
<td>==</td>
<td>等于</td>
<td>x == y</td>
</tr>
<tr>
<td>!=</td>
<td>不等于</td>
<td>x != y</td>
</tr>
<tr>
<td>></td>
<td>大于</td>
<td>x > y</td>
</tr>
<tr>
<td><</td>
<td>小于</td>
<td>x < y</td>
</tr>
<tr>
<td>>=</td>
<td>大于等于</td>
<td>x >= y</td>
</tr>
<tr>
<td><=</td>
<td>小于等于</td>
<td>x <= y</td>
</tr>
</tbody></table>
Python逻辑运算符
逻辑运算符用于组合条件语句:
<table >
<tbody><tr>
<th style="width:25%">运算符</th>
<th style="width:35%">描述</th>
<th style="width:30%">示例</th>
</tr>
<tr>
<td>and </td>
<td>与</td>
<td>x < 5 and x < 10</td>
</tr>
<tr>
<td>or</td>
<td>或</td>
<td>x < 5 or x < 4</td>
</tr>
<tr>
<td>not</td>
<td>非</td>
<td>not(x < 5 and x < 10)</td>
</tr>
</tbody></table>
Python 身份运算符
身份运算符用于识别两个对象是不是同一个对象,Python通过比较它们的内存位置是否相同来判断:
<table >
<tbody><tr>
<th style="width:25%">运算符</th>
<th style="width:35%">描述</th>
<th style="width:30%">示例</th>
</tr>
<tr>
<td>is </td>
<td>如果是同一个对象返回真</td>
<td>x is y</td>
</tr>
<tr>
<td>is not</td>
<td>如果不是同一个对象返回真</td>
<td>x is not y</td>
</tr>
</tbody></table>
Python 成员运算符
成员运算符用于判断序列中是否包含某个对象:
<table >
<tbody><tr>
<th style="width:25%">运算符</th>
<th style="width:35%">描述</th>
<th style="width:30%">示例</th>
</tr>
<tr>
<td>in </td>
<td>如果序列中包含对象,返回真</td>
<td>x in y</td>
</tr>
<tr>
<td>not in</td>
<td>如果序列中不包含对象,返回真</td>
<td>x not in y</td>
</tr>
</tbody></table>
Python 位运算符
位运算符用于对(二进制)数值进行位运算:
<table >
<tbody><tr>
<th style="width:10%">运算符</th>
<th style="width:15%">名称</th>
<th style="width:75%">描述</th>
</tr>
<tr>
<td>& </td>
<td>AND</td>
<td>如果两个位都是1,结果为1</td>
</tr>
<tr>
<td>|</td>
<td>OR</td>
<td>如果两个位中的一个为1,结果为1</td>
</tr>
<tr>
<td> ^</td>
<td>XOR</td>
<td>如果两个位中只有一个是1,结果为1</td>
</tr>
<tr>
<td>~ </td>
<td>NOT</td>
<td>反转所有位</td>
</tr>
<tr>
<td><<</td>
<td>Zero fill left shift</td>
<td>左移位,右边填0,左边移除</td>
</tr>
<tr>
<td>>></td>
<td>Signed right shift</td>
<td>右移位,左边复制首位的值,右边移除</td>
</tr>
</tbody></table>