Python 运算符及优先级实例介绍
最编程
2024-01-02 17:38:45
...
什么是运算符?
答案很简单,可以使用表达式4+5等于9。这里4和5被称为操作数+被称为运营商。 Python语言支持以下类型的运算符。
- 算术运算符
- 比较操作符
- 逻辑(或关系)
- 运算符
- 赋值运算符
- 有条件(或三元)运算符
让我们对所有运算符逐一看看。
Python的算术运算符: 假设变量拥有10和变量b则持有20:
操作符 | 描述 | 例子 |
---|---|---|
+ | 加法 - 增加了对操作两边的值 | a + b will give 30 |
- | 减法 - 从左边操作数减去右边操作 | a - b will give -10 |
* | 乘法 - 操作两边的值相乘 | a * b will give 200 |
/ | 除法 - 右边操作数除以左边 操作 | b / a will give 2 |
% | 模 - 除以右边操作数,并返回其余的左手操作 | b % a will give 0 |
** | 指数 - 执行指数(电源)计算操作符 | a**b will give 10 to the power 20 |
// | Floor Division - 操作数,结果是在小数点后的数字将被删除商数分工。 | 9//2 is equal to 4 and 9.0//2.0 is equal to 4.0 |
Python的比较运算符:
假设变量拥有10和变量b则持有20:
操作符 | 描述 | 例子 |
---|---|---|
== | 检查,如果两个操作数的值等于或没有,如果是则条件为真。 | (a == b) is not true. |
!= | 检查,如果两个操作数的值是相等的,如果值不相等,则条件为真。 | (a != b) is true. |
<> | 检查,如果两个操作数的值是相等的,如果值不相等,则条件为真。 | (a <> b) is true. This is similar to != operator. |
> | 检查,如果左操作数的值是大于右操作数的值,如有条件变为真。 | (a > b) is not true. |
< | 检查,如果左操作数的值小于右操作数的值,如果是则条件为真。 | (a < b) is true. |
>= | 检查,如果左操作数的值大于或等于右边的操作数的值,如果是则条件为真。 | (a >= b) is not true. |
<= | 检查,如果左操作数的值是小于或等于右边的操作数的值,如果是则条件为真。 | (a <= b) |
Python赋值运算符:
假设变量拥有10和变量b则持有20:
操作符 | 描述 | Example |
---|---|---|
= | 简单的赋值操作符,左侧操作数分配从右边的操作数的值. | c = a + b will assigne value of a + b into c |
+= | 添加和赋值操作符,它增加了右操作数的左操作数,并将结果赋给左操作数. | c += a is equivalent to c = c + a |
-= | 减和赋值操作符,它减去右边的操作数从左边的操作数,并将结果赋给左操作数 | c -= a is equivalent to c = c - a |
*= | 乘和赋值操作符,它乘以右边的操作数与左操作数,并将结果赋给左操作数 | c *= a is equivalent to c = c * a |
/= | 除和赋值操作符,它分为左操作数与右边的操作数,并将结果赋给左操作数 | c /= a is equivalent to c = c / a |
%= | 弹性模量和赋值操作符,它需要使用两个操作数,并将结果赋给左操作数的模 | c %= a is equivalent to c = c % a |
**= | 指数和赋值运算符,执行指数的计算和分配价值左操作数 | c **= a is equivalent to c = c ** a |
//= | Dividion和分配一个值,执行运营商地板分工和分配的左操作数的值 | c //= a is equivalent to c = c // a |
Python位运算符:
位运算符位和执行位操作位。 假设如果= 60和b=13;现在他们在二进制格式将如下:
a = 0011 1100
b = 0000 1101
-----------------
a&b = 0000 1100
a|b = 0011 1101
a^b = 0011 0001
~a = 1100 0011
有以下位运算符是由Python语言的支持
操作符 | 描述 | Example |
---|---|---|
& | 二进制和操作员会将位的结果,如果它在两个操作数存在。 | (a & b) will give 12 which is 0000 1100 |
| | 二进制OR运算符复制了一下,如果它存在eather操作。 | (a | b) will give 61 which is 0011 1101 |
^ | 如果它被设置在一个操作数,但不能同时二进制XOR运算的位复制。 | (a ^ b) will give 49 which is 0011 0001 |
~ | 二进制的补运算符是一元,并有'翻转'位效应。 | (~a ) will give -60 which is 1100 0011 |
<< | 二进位向左移位运算符。左操作数的值被移到左由右操作数指定的位数。 | a << 2 will give 240 which is 1111 0000 |
>> | 二进制右移运算符。左操作数的值被移到右,由右操作数指定的位数。 | a >> 2 will give 15 which is 0000 1111 |
Python逻辑运算符:
有下列Python语言所支持的逻辑运算符
假设变量拥有10和变量b则持有20:
操作符 | 描述 | Example |
---|---|---|
and | 所谓的逻辑AND运算。如果两个操作数都是真实的,然后再条件变为真。 | (a and b) is true. |
or | 被称为逻辑OR运算。如果任何两个操作数都是非零然后再条件变为真。 | (a or b) is true. |
not | 所谓逻辑非运算符。使用以扭转其操作数的逻辑状态。如果条件为真,则逻辑非运算符将作出虚假。 | not(a |
Python成员操作符:
除了运营商在以前讨论,Python有成员操作符,测试序列中的成员,如字符串,列表或元组。 有两个成员资格的经营者说明如下:
操作 | 描述 | 例子 |
---|---|---|
in | 计算结果为true,如果发现在指定的顺序,否则返回false变量。 | y 在 X中,结果在1如果x是一个序列y的成员。 |
not in | 计算结果为true,如果没有找到变量在指定的顺序,否则返回false。 | x not in y, here not in results in a 1 if x is a member of sequence y. |
Python身份运算符:
身份运算符比较两个对象的内存位置。 有两种身份运算符解释如下:
Operator | Description | Example |
---|---|---|
is | 计算结果为true,如果操作点两侧的变量相同的对象,否则返回false. | x is y, here is results in 1 if id(x) equals id(y). |
is not | 计算结果为false,如果操作点两侧的变量相同的对象和真实,否则真. | x is not y, here is not results in 1 if id(x) is not equal to id(y). |
Python运算符优先级
下表列出了所有运营商从最高优先级到最低。
Operator | Description |
---|---|
** | Exponentiation (raise to the power) |
~ + - | Ccomplement, unary plus and minus (method names for the last two are +@ and -@) |
* / % // | Multiply, divide, modulo and floor division |
+ - | Addition and subtraction |
>> << | Right and left bitwise shift |
& | Bitwise 'AND' |
^ | | Bitwise exclusive `OR' and regular `OR' |
<= < > >= | Comparison operators |
<> == != | Equality operators |
= %= /= //= -= += *= **= | Assignment operators |
is is not | Identity operators |
in not in | Membership operators |
not or and | Logical operators |
上一篇: JAVA的位移运算符>>和>>>简介
推荐阅读
-
[人工智能] 启发式搜索:采用 A* 原则的贪婪最佳优先级搜索及 Python 代码实现
-
Python 学习:注释和运算符 - python 运算符优先级
-
C++编程:实战教程 - 实现栈与队列(Deque) 容器适配器,探讨Stack和Queue的实现及Queueue(Deque)的问题。深入理解:如何模拟实现Priority Queue (优先级队列) - 实例与解题,以及仿射函数入门(第一部分)
-
从零基础到Python大神之旅 - 第一章:Python实战应用(三)- 数据可视化及常用库介绍
-
使用Simulink实现粒子群优化(PSO)调节PID控制器参数 - 介绍与实例解析及配套代码
-
我们是如何了解人脑构造的:探究神经元、神经网络及学习运作机制的奥秘,还有Python实现类似人脑结构的简单实例
-
介绍使用Python语言中的三元运算符技巧
-
Python中的逻辑运算符:或、且和非的用法及区别
-
深入解析Python中JWT的使用及介绍
-
常见的 35 个 Python 面试题及答案-Q.30. 解释 Python 中的位运算符