Python operators allow us to do common processing on variables. We will look into different types of python operators with examples and also operator precedence.
Python运算符允许我们对变量进行通用处理。 我们将通过示例和运算符优先级研究不同类型的python运算符。
Python运算子 (Python Operators)
Python Operators are the special symbols that can manipulate values of one or more operands.
Python运算符是可以操纵一个或多个操作数的值的特殊符号。
Python运算子类型 (Python Operator Types)
Python operators can be classified into several categories.
Python运算符可分为几类。
- Arithmetic Operators 算术运算符
- Logical Operators 逻辑运算符
- Comparison Operators 比较运算符
- Bitwise Operators 按位运算符
- Assignment Operators 赋值运算符
Python算术运算符 (Python Arithmetic Operators)
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 |
/ | used to divide two numbers | div = b/a |
% | modulus operator, returns the remainder of division | mod = a%b |
** | exponent operator |
操作员 | 描述 | 例 |
---|---|---|
+ | 用于加两个数字 | 总和= a + b |
– | 用于减法 | 差异= a – b |
* | 用于将两个数字相乘。 如果将字符串和int相乘,则将字符串重复int次。 | mul = a * b >>>“嗨” * 5 |
/ | 用于除以两个数字 | div = b / a |
% | 模运算符,返回除法的余数 | mod = a%b |
** | 指数算子 |
#create two variables
a=100
b=200
# addition (+) operator
print(a+b)
# subtraction (-) operator
print(a-b)
# multiplication (*) operator
print(a*b)
# division (/) operator
print(b/a)
# modulus (%) operator
print(a%b) # prints the remainder of a/b
# exponent (**) operator
print(a**b) #prints a^b
Output:
输出:
Python比较运算符 (Python Comparison Operators)
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 |
操作员 | 描述 | 例 |
---|---|---|
== | 如果两个操作数相等,则返回True,否则返回False。 | 标志= a == b |
!= | 如果两个操作数不相等,则返回True,否则返回False。 | 标志= a!= b |
> | 如果左操作数大于右操作数,则返回True,否则返回False。 | 标志= a> b |
< | 如果左操作数小于右操作数,则返回True,否则返回False。 | 标志= a <b |
> = | 如果左操作数大于或等于右操作数,则返回True,否则返回False。 | 标志= a> b |
<= | 如果左操作数小于或等于右操作数,则返回True,否则返回False。 | 标志= a <b |
# create two variables
a=100
b=200
# (==) operator, checks if two operands are equal or not
print(a==b)
# (!=) operator, checks if two operands are not equal
print(a!=b)
# (>) operator, checks left operand is greater than right operand or not
print(a>b)
# (<) operator, checks left operand is less than right operand or not
print(a<b)
#(>=) operator, checks left operand is greater than or equal to right operand or not
print(a>=b)
# (<=) operator, checks left operand is less than or equal to right operand or not
print(a<=b)
Python按位运算符 (Python Bitwise Operators)
Operator | Description | Example |
---|---|---|
& | Binary AND Operator | x = 10 & 7 = 2 |
| | Binary OR Operator | x = 10 | 7 = 15 |
^ | 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 |
操作员 | 描述 | 例 |
---|---|---|
和 | 二进制与运算符 | x = 10&7 = 2 |
| | 二进制或运算符 | x = 10 | 7 = 15 |
^ | 二进制XOR运算符 | x = 10 ^ 7 = 13 |
〜 | 二进制ONE补码运算符 | x =〜10 = -11 |
<< | 二进制左移运算符 | x = 10 << 1 = 20 |
>> | 二元右移运算符 | x = 10 >> 1 = 5 |
#create two variables
a=10 # binary 1010
b=7 # binary 0111
# Binary AND (&) operator, done binary AND operation
print(a&b)
# Binary OR (|) operator, done binary OR operation
print(a|b)
# Binary XOR (^) operator, done binary XOR operation
print(a^b)
# Binary ONEs Compliment (~) operator, done binary One's Compliment operation
print(~a)
# Binary Left Shift (<<) operator, done binary Left Shift operation
print(a<<1)
# Binary Right Shift (>>) operator, done binary Right Shift operation
print(a>>1)
Python逻辑运算符 (Python Logical Operators)
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 |
操作员 | 描述 | 例 |
---|---|---|
和 | 逻辑与运算符 | 标志= exp1和exp2 |
要么 | 逻辑或运算符 | 标志= exp1或exp2 |
不 | 逻辑非运算符 | 标志=不(True)= False |
#take user input as int
a=int(input())
# logical AND operation
if a%4==0 and a%3==0:
print("divided by both 4 and 3")
# logical OR operation
if a%4==0 or a%3==0:
print("either divided by 4 or 3")
# logical NOT operation
if not(a%4==0 or a%3==0):
print("neither divided by 4 nor 3")
Python赋值运算符 (Python Assignment Operators)
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) |
操作员 | 描述 |
---|---|
+ = | a + = b等效于a = a + b |
* = | a * = b等效于a = a * b |
/ = | a / = b等效于a = a / b |
%= | a%= b等效于a = a%b |
** = | a ** = b等效于a = a ** b(指数运算符) |
// = | a // = b等于a = a // b(底数除法) |
# take two variable, assign values with assignment operators
a=3
b=4
print("a: "+str(a))
print("b: "+str(b))
# it is equivalent to a=a+b
a+=b
print("a: "+str(a))
print("b: "+str(b))
# it is equivalent to a=a*b
a*=b
print("a: "+str(a))
print("b: "+str(b))
# it is equivalent to a=a/b
a/=b
print("a: "+str(a))
print("b: "+str(b))
# it is equivalent to a=a%b
a%=b
print("a: "+str(a))
print("b: "+str(b))
# it is equivalent to a=a**b ( exponent operator)
a**=b
print("a: "+str(a))
print("b: "+str(b))
# it is equivalent to a=a//b ( floor division)
a//=b
print("a: "+str(a))
print("b: "+str(b))
Python运算符优先级 (Python Operator Precedence)
Precedence of python operators means the priority level of operators. This becomes vital when an expression has multiple operators in it. For example consider the following expression:
python运算符的优先级是指运算符的优先级。 当表达式中包含多个运算符时,这一点至关重要。 例如,考虑以下表达式:
>>> 2+3*4
Now, what do you think the series of operation would be? We can add 2 and 3, then multiply the result by 4. Also, we can multiply 3 and 4 first, then add 2 with it. Here we can see that the operators’ precedence is important.
现在,您认为这一系列操作是什么? 我们可以将2和3相加,然后将结果乘以4。此外,我们可以先将3和4相乘,然后再加上2。 在这里我们可以看到,运算符的优先级很重要。
Below is a list of operators indicating the precedence level. It’s in descending order. That means the upper group has more precedence than that of the lower group.
以下是指示优先级的运算符列表。 它是降序排列的。 这意味着上层人群比下层人群具有更高的优先级。
- 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-
=
分配运算符-=
Reference: Official Python Documentation
参考: 官方Python文档