Python 运算器允许我们对变量进行共同处理,我们将以示例和运算器优先级来研究不同类型的运算器,它们是能够操纵一个或多个运算符的值的特殊符号。
Python 操作员列表
Python 操作员可以分为几个类别。
- 配置运算符
- 算术运算符
- 逻辑运算符
- 比较运算符
- Bitwise运算符
Python 任务操作员
分配操作员包括等于签名 (=)的基本分配操作员。
但是,为了简化代码,并减少冗余,Python还包括算术分配操作员。
这包括用于添加分配的 += Python 操作员, //=地板分配分配操作员等。
以下是 Python 中的所有算术分配运算符的列表。
Operator | Description |
---|---|
+= | a+=b is equivalent to a=a+b |
*= | a*=b is equivalent to a=a*b |
/= | a/=b is equivalent to a=a/b |
%= | a%=b is equivalent to a=a%b |
**= | a**=b is equivalent to a=a**b (exponent operator) |
//= | a//=b is equivalent to a=a//b (floor division) |
使用分配操作员
1# take two variable, assign values with assignment operators
2a=3
3b=4
4
5print("a: "+str(a))
6print("b: "+str(b))
7
8# it is equivalent to a=a+b
9a+=b
10
11print("a: "+str(a))
12print("b: "+str(b))
13
14# it is equivalent to a=a*b
15a*=b
16print("a: "+str(a))
17print("b: "+str(b))
18
19# it is equivalent to a=a/b
20a/=b
21print("a: "+str(a))
22print("b: "+str(b))
23
24# it is equivalent to a=a%b
25a%=b
26print("a: "+str(a))
27print("b: "+str(b))
28
29# it is equivalent to a=a**b ( exponent operator)
30a**=b
31print("a: "+str(a))
32print("b: "+str(b))
33
34# it is equivalent to a=a//b ( floor division)
35a//=b
36print("a: "+str(a))
37print("b: "+str(b))
Python 算术运算器
Operator | Description | Example |
---|---|---|
+ | used to add two numbers | sum = a + b |
– | used for subtraction | difference = a – b |
* | used to multiply two numbers. If a string and int is multiplied then the string is repeated the int times. | mul = a*b>>> Hi *5 |
‘HiHiHiHiHi’ | ||
/ | used to divide two numbers | div = b/a |
% | modulus operator, returns the remainder of division | mod = a%b |
** | exponent operator |
1#create two variables
2a=100
3b=200
4
5# addition (+) operator
6print(a+b)
7
8# subtraction (-) operator
9print(a-b)
10
11# multiplication (*) operator
12print(a*b)
13
14# division (/) operator
15print(b/a)
16
17# modulus (%) operator
18print(a%b) # prints the remainder of a/b
19
20# exponent (**) operator
21print(a**b) #prints a^b
Output:
Python 操作器比较
Operator | Description | Example |
---|---|---|
== | returns True if two operands are equal, otherwise False. | flag = a == b |
!= | returns True if two operands are not equal, otherwise False. | flag = a != b |
> | returns True if left operand is greater than the right operand, otherwise False. | flag = a > b |
< | returns True if left operand is smaller than the right operand, otherwise False. | flag = a < b |
>= | returns True if left operand is greater than or equal to the right operand, otherwise False. | flag = a > b |
<= | returns True if left operand is smaller than or equal to the right operand, otherwise False. | flag = a < b |
1# create two variables
2a=100
3b=200
4
5# (==) operator, checks if two operands are equal or not
6print(a==b)
7
8# (!=) operator, checks if two operands are not equal
9print(a!=b)
10
11# (>) operator, checks left operand is greater than right operand or not
12print(a>b)
13
14# (<) operator, checks left operand is less than right operand or not
15print(a<b)
16#(>=) operator, checks left operand is greater than or equal to right operand or not
17print(a>=b)
18
19# (<=) operator, checks left operand is less than or equal to right operand or not
20print(a<=b)
Python Bitwise 操作员
Operator | Description | Example |
---|---|---|
& | Binary AND Operator | x = 10 & 7 = 2 |
Binary OR Operator | ||
^ | Binary XOR Operator | x = 10 ^ 7 = 13 |
~ | Binary ONEs Compliment Operator | x = ~10 = -11 |
<< | Binary Left Shift operator | x = 10<<1 = 20 |
>> | Binary Right Shift Operator | x = 10>>1 = 5 |
1#create two variables
2a=10 # binary 1010
3b=7 # binary 0111
4
5# Binary AND (&) operator, done binary AND operation
6print(a&b)
7
8# Binary OR (|) operator, done binary OR operation
9print(a|b)
10
11# Binary XOR (^) operator, done binary XOR operation
12print(a^b)
13
14# Binary ONEs Compliment (~) operator, done binary One's Compliment operation
15print(~a)
16
17# Binary Left Shift (<<) operator, done binary Left Shift operation
18print(a<<1)
19# Binary Right Shift (>>) operator, done binary Right Shift operation
20print(a>>1)
Python 逻辑操作器
Operator | Description | Example |
---|---|---|
and | Logical AND Operator | flag = exp1 and exp2 |
or | Logical OR Operator | flag = exp1 or exp2 |
not | Logical NOT Operator | flag = not(True) = False |
1#take user input as int
2a=int(input())
3
4# logical AND operation
5
6if a%4==0 and a%3==0:
7 print("divided by both 4 and 3")
8
9# logical OR operation
10if a%4==0 or a%3==0:
11 print("either divided by 4 or 3")
12
13# logical NOT operation
14if not(a%4==0 or a%3==0):
15 print("neither divided by 4 nor 3")
Python 操作器先行性
这些运算符的优先级意味着运算符的优先级级别. 当一个表达式有多个运算符时,这变得至关重要。
1>>> 2+3*4
现在,你认为操作序列会是什么呢?我们可以添加2和3,然后以4来倍增结果,此外,我们可以先倍增3和4,然后添加2。
下面列出了指示优先级级的操作员列表,它以下降顺序进行,这意味着上方的组比下方的组具有更大的优先级。
- Parenthesis –
()
- Exponentiation –
**
- Compliment, unary plus and minus –
~
,+
,-
- Multiply, Divide, modulo –
*
,/
,%
- Addition and Subtraction –
+
,-
- Right and Left Shift –
>>
,<<
- Bitwise AND –
&
- Bitwise OR and XOR –
|
,^
- Comparison Operators –
==
,!=
,>
,<
,>=
,<=
- Assignment Operator-
=